假設我有一個模型狗和每隻狗都有2個Cat「敵人」,enemy1
和enemy2
,我如何編寫遷移文件以便我可以撥打dog.enemy1
來檢索第一個敵人和dog.enemy2
找回第二個敵人? 我嘗試這樣做:Rails中的同一模型的多個關聯
create_table :dog do |t|
t.string :name
t.timestamps null: false
end
add_index :dog, :name
add_foreign_key :dogs, :cats, column: :enemy1_id
add_foreign_key :dogs, :cats, column: :enemy2_id
end
我還與t.references
方法嘗試過,但無法得到它的工作。幾個小時一直在處理這個問題。並且它在開發中工作正常,但在Heroku Postgres上沒有問題。 我得到的錯誤是
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:column "enemy1_id" referenced in foreign key constraint does not exist.
任何想法?