0

在本地,我的迁移很好(尽管我使用的是 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

以前的 Collaborations 迁移是

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”

4

1 回答 1

1

你用错误的参数调用外国人的方法。第一个参数是引用的表名而不是引用的列名。而且由于您的列名与表名不匹配,因此您还需要:column选项。像这样的东西:

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

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

错误信息:

关系“member1_id”不存在

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

于 2013-03-21T20:31:21.440 回答