Reputation: 280
I created two references in a migration that are aliases for a reference to my User table:
class CreateInvitations < ActiveRecord::Migration[5.0]
def change
create_table :invitations do |t|
t.references :owner, references: :user, foreign_key: true # the owner
t.references :invitee, references: :user, foreign_key: true # the invitee
t.references :core_bot, foreign_key: true # the associated page (core_bot_active)
t.string :email
t.string :token
t.timestamps
end
end
end
In my User model:
has_many :invitations, foreign_key: :owner_id
has_many :invitations, foreign_key: :invitee_id, dependent: :destroy
In my Invitation model:
belongs_to :owner, class_name: :User
belongs_to :invitee, class_name: :User
Everything works well in development but when I try to migrate in production with Heroku heroku run rake db:migrate
, I get the following error:
PG::UndefinedTable: ERROR: relation "owners" does not exist : CREATE TABLE "invitations" ("id" serial primary key, "owner_id" integer, "invitee_id" integer, "core_bot_id" integer, "email" character varying, "token" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_59e24979a9" FOREIGN KEY ("owner_id") REFERENCES "owners" ("id") , CONSTRAINT "fk_rails_00204dc74b" FOREIGN KEY ("invitee_id") REFERENCES "invitees" ("id") , CONSTRAINT "fk_rails_34505bdb65" FOREIGN KEY ("core_bot_id") REFERENCES "core_bots" ("id") )
I tried without references: :user
but I get the same error.
Any idea what's wrong here?
Upvotes: 3
Views: 6738
Reputation: 36860
Your development is probably an sqLite database but Heroku uses PostgreSQL and the interpretation of the migration is generating a foregn key to owners
Write the migration like this instead...
class CreateInvitations < ActiveRecord::Migration[5.0]
def change
create_table :invitations do |t|
t.references :owner, index: true # the owner
t.references :invitee, index: true # the invitee
t.references :core_bot, foreign_key: true # the associated page (core_bot_active)
t.string :email
t.string :token
t.timestamps
end
add_foreign_key :invitations, :users, column: :owner_id
add_foreign_key :invitations, :users, column: :invitee_id
end
end
It's one of the risks with developing using a different database product than the production implementation. Migrations may not work exactly the same. If planning to deploy to Heroku you should look at using postgreSQL in development.
Upvotes: 10
Reputation: 1060
try removing foreign_key: true from the options, since we are giving it a references, I think we don't need a foreign_key: true option
class CreateInvitations < ActiveRecord::Migration[5.0]
def change
create_table :invitations do |t|
t.references :owner, # the owner
t.references :invitee, # the invitee
t.references :core_bot # the associated page (core_bot_active)
t.string :email
t.string :token
t.timestamps
end
end
end
Upvotes: -1
Reputation: 331
I don't how to fix your problem. But I allways creating migrations for Postgres DB with foreign keys like this:
def change
create_table :invitations do |t|
t.integer :owner_id
t.integer :invitee_id
t.references :core_bot, foreign_key: true # the associated page (core_bot_active)
t.string :email
t.string :token
t.timestamps
end
add_index :invitations, :owner_id
add_foreign_key :invitations, :users, column: :owner_id
add_index :invitations, :invitee_id
add_foreign_key :invitations, :users, column: :invitee_id
end
Upvotes: 1