首页 > 解决方案 > Upgrading to Rails 5.2, can't boot, ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation does not exist

问题描述

With a new database, running bundle exec rails db:structure:load or rails db:create or rails db:migrate or bin/rails db:setup throws

rails aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "clients" 
does not exist
LINE 8:                WHERE a.attrelid = '"clients"'::regclass
...etc                                      ^

I'd be happy just knowing how to debug this. At some point, some code is calling the Client model, but it's invisible. What could be requiring the clients table to exist pre-boot?

Edits:

No migrations erased, no. The app boots fine locally where there's a database: this first occurs in CI where it's booting from scratch.

Here's the stacktrace https://gist.github.com/EmmaB/01a9322ab66f258a5ebd6883a441251f showing another missing table after I commented out the following code in a Flipper initializer:

Client::Identifiers::IDENTIFER_ID_MAPPING.each do |client_identifier, client_id|
  Flipper.register(client_identifier) do |actor|
    actor&.client_id == client_id
  end
end

But that initialiser code shouldn't run in boot when you're loading the structure, right? And indeed doesn't run in Rails 4.2.10 (upgrading from that to 5.2).

标签: ruby-on-rails

解决方案


在初始化器中包装模型调用

Rails.configuration.after_initialize do
  # Model calls 
end

让应用程序启动。初始化程序必须在启动过程中比在 Rails 4 中更早地调用。编辑:事实上,这里是线程https://github.com/rails/rails/issues/32870


推荐阅读