在遷移我收到以下錯誤信息:PG :: UndefinedTable:ERROR:關係 「......」 不存在
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
而對於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
組織模型文件包括:belongs_to :actioncode
。
雖然actioncodes模型文件包含: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")
表名是actioncodes。我已將其遷移文件添加到原始文章 – Nick
看起來'CreateOrganizations'遷移正在執行'CreateActioncodes'之前運行。 - 'CreateActioncodes'將首先運行,從而確保'actioncodes'表存在。 –
你可能會建議我應該如何改變它?我檢查並確認SQL代碼確實在Actioncodes之前創建了組織。 – Nick