在Heroku上运行迁移时,我得到PG ::错误:错误:关系“member1_id”不存在

时间:2013-03-21 19:22:02

标签: ruby-on-rails postgresql foreigner

在本地,我的迁移很好(虽然我正在使用SQLite。将在开发时切换到postgresql asap)。

使用

重置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”添加不必要的“_id”

1 个答案:

答案 0 :(得分:1)

您使用错误的参数调用foreigner's方法。第一个参数是引用的表名,而不是引用列名。由于您的列名称与表名称不匹配,因此您还需要:column个选项。像这样:

t.foreign_key :users, :column => :member1_id, :dependent => :delete

假设:users是您的:member1_id:member2_id列应指向的表名。

错误消息:

  

关系“member1_id”不存在

告诉你PostgreSQL正在寻找一个名为member1_id的表,但找不到它。