Locally, my migrations are fine (although I'm using SQLite. Will switch to postgresql on development asap).
After resetting the database on Heroku with
heroku pg:reset DATABASE
I ran
heroku run rake db:migrate
But I am getting the following error after a migration:
== 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
Here is that migration:
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
Previous migrations for Collaborations are
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
and
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
Which are run in that order. Why is this error coming up on Heroku? Specifically, it looks like PG is adding an unnecessary "_id" to "member1_id"
You're calling foreigner's methods with the wrong arguments. The first argument is the referenced table name not the referencing column name. And since your column names don't nicely match the table names you'll need :column
options as well. Something like this:
t.foreign_key :users, :column => :member1_id, :dependent => :delete
That assumes that :users
is the table name that your :member1_id
and :member2_id
columns should be pointing at.
The error message:
relation "member1_id" does not exist
tells you that PostgreSQL is looking for a table called member1_id
but can't find it.