3

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?

nico_lrx
  • 715
  • 1
  • 19
  • 36

3 Answers3

10

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.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
1

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
Jokūbas
  • 321
  • 5
  • 14
  • 1
    Thank you it works! I accept the answer of Steve as he gives a bit more details about the difference of database used. ;) – nico_lrx Dec 09 '17 at 12:07
-1

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
Dyaniyal Wilson
  • 1,012
  • 10
  • 14
  • I don't think it would be the answer because you are not creating the FK on the `invitations` table. And that will cause that the validation is going to be only on the ORM layer but not at a DB level and data could be inconsistent. You should also add the corresponding FK and Indexes manually, something like this: ```add_foreign_key :invitations, :users, column: :owner_id``` ```add_foreign_key :invitations, :users, column: :invitee_id``` ```add_index :invitations, :owner_id``` ```add_index :invitations, :invitee_id``` – alexventuraio Mar 10 '19 at 17:57