Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
PG::UndefinedTable: ERROR:  relation "musicians" does not exist
: ALTER TABLE "orders" ADD CONSTRAINT "fk_rails_ad134589be"
FOREIGN KEY ("musician_id")
  REFERENCES "musicians" ("id")
   (0.9ms)  ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR:  relation "musicians" does not exist
: ALTER TABLE "orders" ADD CONSTRAINT "fk_rails_ad134589be"
FOREIGN KEY ("musician_id")

Here is a link to the entire log: https://gist.github.com/helloravi/2cb69e0927e63e186b09

The following is the migration which does not get executed. The error is displayed below the migration code

class CreateAlbums < ActiveRecord::Migration
  def change
    create_table :albums do |t|
      t.string :album_name
      t.references :musician, index: true, foreign_key: true
      t.timestamps null: false
    add_foreign_key :albums, :users, column: :musician_id

I have a users table with a musician column which is boolean(some users are musicians)

I even tried using add_foreign_key and still I am not able to figure out what the problem is.

I tried rake db:schema:load and it worked. I want to be able to make rake db:migrate work because I need to be able to migrate in production.

` belongs_to :musician, class_name: User.name` ` has_many :songs` ` has_many :order_items` – Ravikanth Andhavarapu Feb 12, 2016 at 10:19

SQLite does not check foreign keys, it simply ignores them. But PostgreSQL is very strict and raises an error when the foreign key constraint is not valid.

Rails foreign_key does not support what you want it to do. When you write t.references :musician then there must be a musicians table. But you want the foreign key to point to a users table.

I see two options:

  • Use t.references :users and rename that association in your albums.rb like this:

    belongs_to :musician, class_name: 'User', foreign_key: 'user_id'
    
  • Or: you just use t.integer :musician_id instead of references and define the foreign key constraint manually with an execute 'ALTER TABLE ...'

  • 1. I will probably go with option one. I am already using the association the way you suggested. 2. How would I manually execute the execute 'ALTER TABLE ...' in heroku ? 3. Would it work if I deleted the line with t.references and used add_foreign_key line in the migration? – Ravikanth Andhavarapu Feb 12, 2016 at 10:15 Changing your migration to the following should work:

    class CreateAlbums < ActiveRecord::Migration
      def change
        create_table :albums do |t|
          t.string :album_name
          t.integer :musician_id
          t.timestamps null: false
        add_foreign_key :albums, :users, column: :musician_id
        add_index :albums, :musician_id
                    What would be the command which would create a migration of what you mentioned?(I was also planning to do what Speckerman said)
    – Ravikanth Andhavarapu
                    Feb 12, 2016 at 10:23
                    You could just edit the migration. The existing migration will not work on postgresql. You will have to make a change to the existing migration. Making this change will not effect what happens with sqlite3.
    – Satyaram B V
                    Feb 12, 2016 at 10:27
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •