在迁移时,我收到以下错误消息:
PG::UndefinedTable: ERROR: relation "actioncodes" does not exist
: ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_4ecaa2493e"
FOREIGN KEY ("actioncode_id")
REFERENCES "actioncodes" ("id")
我有以下用于组织的迁移文件:
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
对于Actioncode,我具有迁移文件:
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
组织模型文件包括:belongs_to :actioncode
。
而动作代码模型文件包括:has_many :organizations
。
任何想法可能导致错误消息吗?
如果我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")
因此,由于CreateOrganizations
迁移是在执行之前进行的,因此问题正在发生CreateActioncodes
。
CreateActioncodes
首先运行,从而确保该action codes
表存在。
迁移的运行顺序基于迁移的时间戳-如文件名所示。20141014183645_create_users.rb
将20141014205756_add_index_to_users_email.rb
在第二个时间戳之前运行-在第一个时间戳之后运行20141014205756
- 20141014183645
。
确保CreateOrganizations
迁移的时间戳记在迁移之后CreateActioncodes
。
您可以手动更改文件名中的时间戳。或删除这些迁移文件,然后以正确的顺序创建它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句