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
PG::UndefinedTable: ERROR: relation "..." does not exist
提问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: true
from 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 CreateOrganizations
migration is being run before CreateActioncodes
is executed.
所以这个问题正在发生,因为CreateOrganizations
迁移是在执行之前运行的CreateActioncodes
。
CreateActioncodes
is to be run first thereby ensuring that the action codes
table 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.rb
will run before 20141014205756_add_index_to_users_email.rb
as the timestamp of the second one - 20141014205756
is 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 CreateOrganizations
migration is after that of CreateActioncodes
migration.
确保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: true
in 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 actioncode
column and that you can't remove entries from the actioncodes
table 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 organizations
before actioncodes
so all you need to do is rename the CreateOrganizations
migration 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 CreateOrganizations
timestamp to the CreateActioncodes
timestamp 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:prepare
in the terminal before running rspec.
我也收到了这个错误。如果您使用测试数据库来运行 rspec,请确保rake db:test:prepare
在运行 rspec 之前在终端中运行。