中添加了一个不必要的"_id"。
在本地,我的迁移是好的(尽管我使用SQLite)。将尽快切换到postgresql开发)。
使用
重置Heroku数据库后heroku pg:reset DATABASE
我跑
heroku run rake db:migrate
但是我在迁移后得到以下错误:
== AddForeignKeysToCollaborations: migrating =================================
-- change_table(:collaborations)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "member1_id" does not exist
: ALTER TABLE "collaborations" ADD CONSTRAINT "collaborations_member1_id_id_fk" FOREIGN KEY ("member1_id_id") REFERENCES "member1_id"(id) ON DELETE CASCADE
迁移:
class AddForeignKeysToCollaborations < ActiveRecord::Migration
def change
change_table :collaborations do |t|
t.foreign_key :member1_id, dependent: :delete
t.foreign_key :member2_id, dependent: :delete
end
end
end
以前的协作迁移是
class CreateCollaborations < ActiveRecord::Migration
def change
create_table :collaborations do |t|
t.integer :user_id
t.integer :collaborator_id
t.timestamps
end
add_index :collaborations, :collaborator_id
add_index :collaborations, [:user_id, :collaborator_id], unique: true
end
end
和
class UpdateCollaborations < ActiveRecord::Migration
def change
change_table :collaborations do |t|
t.rename :user_id, :member1_id
t.rename :collaborator_id, :member2_id
t.string :status
end
add_index :collaborations,:member1_id
add_index :collaborations,:member2_id
end
end
按此顺序运行。为什么Heroku会出现这个错误?具体来说,PG似乎在"member1_id"
使用错误的参数调用外国人的方法。第一个参数是被引用的表名,而不是引用的列名。由于列名与表名不能很好地匹配,因此还需要:column
选项。像这样:
t.foreign_key :users, :column => :member1_id, :dependent => :delete
假设:users
是您的:member1_id
和:member2_id
列应该指向的表名。
错误信息:
关系"member1_id"不存在
告诉你PostgreSQL正在寻找一个名为member1_id
的表,但是找不到它