postgresql PG::UndefinedTable:错误:关系“...”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30290250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:54:32  来源:igfitidea点击:

PG::UndefinedTable: ERROR: relation "..." does not exist

ruby-on-railspostgresqlruby-on-rails-4rails-migrations

提问by Nick

On migration I get the following error message:

在迁移时,我收到以下错误消息:

PG::UndefinedTable: ERROR:  relation "actioncodes" does not exist
: ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_4ecaa2493e"
FOREIGN KEY ("actioncode_id")
  REFERENCES "actioncodes" ("id")

I have the following migration file for Organizations:

我有以下组织的迁移文件:

class CreateOrganizations < ActiveRecord::Migration
  def change
    create_table :organizations do |t|
      t.string     :name,         null: false,    limit: 40
      t.references :actioncode,   index: true,    foreign_key: true
      t.boolean    :activated
      t.datetime   :activated_at

      t.timestamps null: false
    end
  end
end

And for Actioncodes I have the migration file:

对于 Actioncodes,我有迁移文件:

class CreateActioncodes < ActiveRecord::Migration
  def change
    create_table :actioncodes do |t|
      t.string  :code,          null: false,  limit: 20
      t.string  :description,                 limit: 255

      t.timestamps null: false
    end
  end
end
class AddIndexToActioncodesCode < ActiveRecord::Migration
  def change
    add_index :actioncodes, :code,  unique: true
  end
end

The organization model file includes: belongs_to :actioncode.

组织模型文件包括:belongs_to :actioncode.

While the actioncodes model file includes: has_many :organizations.

虽然动作代码模型文件包括:has_many :organizations.

Any idea what could be causing the error message?

知道什么可能导致错误消息吗?

If I remove index: true, foreign_key: truefrom the migration file, it migrates without errors. And when I replace that line with the incorrect line t.references :actioncode_id, index: true, foreign_key: true, it gives the error below, where the last line ("ids") suggests Rails somehow seems to have problem with the name of the table?

如果我index: true, foreign_key: true从迁移文件中删除,它会正确迁移。当我用不正确的行替换该行时t.references :actioncode_id, index: true, foreign_key: true,它给出了下面的错误,最后一行(“ids”)表明 Rails 以某种方式似乎对表名有问题?

PG::UndefinedTable: ERROR:  relation "actioncode_ids" does not exist
: ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_604f95d1a1"
FOREIGN KEY ("actioncode_id_id")
  REFERENCES "actioncode_ids" ("id")

回答by Prakash Murthy

So the issue is happening because CreateOrganizationsmigration is being run before CreateActioncodesis executed.

所以这个问题正在发生,因为CreateOrganizations迁移是在执行之前运行的CreateActioncodes

CreateActioncodesis to be run first thereby ensuring that the action codestable exists.

CreateActioncodes将首先运行,从而确保该action codes表存在。

The order in which migrations are run is based on the time stamp of the migration - as indicated in the name of the file. 20141014183645_create_users.rbwill run before 20141014205756_add_index_to_users_email.rbas the timestamp of the second one - 20141014205756is after that of the first one - 20141014183645.

迁移运行的顺序基于迁移的时间戳 - 如文件名称所示。20141014183645_create_users.rb将在20141014205756_add_index_to_users_email.rb作为第二个时间戳之前运行- 在第一个时间戳之后运行20141014205756- 20141014183645

Make sure the time-stamps of the CreateOrganizationsmigration is after that of CreateActioncodesmigration.

确保CreateOrganizations迁移的时间戳在迁移之后CreateActioncodes

Either you could manually change the timestamp in the file names. Or delete these migration files, and create them in the correct order.

您可以手动更改文件名中的时间戳。或者删除这些迁移文件,并按照正确的顺序创建它们。

回答by mu is too short

The foreign_key: truein this line:

foreign_key: true这行:

t.references :actioncode,   index: true,    foreign_key: true

tells Rails to create a foreign key inside the database. A foreign key:

告诉 Rails 在数据库中创建一个外键。一个外键

constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the referential integritybetween two related tables.

约束指定列(或一组列)中的值必须与出现在另一个表的某行中的值匹配。我们说这维护了两个相关表之间的参照完整性

So it is some logic inside the database (where it belongs) that ensures you can't put invalid values in your actioncodecolumn and that you can't remove entries from the actioncodestable that are being used elsewhere.

因此,数据库(它所属的地方)内部的一些逻辑可确保您不能在actioncode列中放置无效值,并且不能从actioncodes表中删除在其他地方使用的条目。

In order to create the constraint, the referenced table (actioncodes) needs to exist before you refer to it. Looks like your migrations are trying to create organizationsbefore actioncodesso all you need to do is rename the CreateOrganizationsmigration file so that its timestamp prefix comes after the one for CreateActioncodes. The prefix is just a timestamp in the format YYYYMMDDhhmmss so change the CreateOrganizationstimestamp to the CreateActioncodestimestamp with one more second.

为了创建约束,被引用的表 ( actioncodes) 需要在您引用它之前存在。看起来您的迁移organizations之前正在尝试创建,actioncodes因此您需要做的就是重命名CreateOrganizations迁移文件,以便其时间戳前缀位于 for 之后CreateActioncodes。前缀只是格式为 YYYYMMDDhhmmss 的CreateOrganizations时间戳,因此将时间戳更改为CreateActioncodes多一秒的时间戳。

回答by Ilene

I was also getting this error. If you are using a test database to run rspec make sure you run rake db:test:preparein the terminal before running rspec.

我也收到了这个错误。如果您使用测试数据库来运行 rspec,请确保rake db:test:prepare在运行 rspec 之前在终端中运行。