0

I have two models Artist and Painting.

I added the following migration and executed rake db:migrate

class AddArtistReferenceToPaintings < ActiveRecord::Migration
  self.up do 
    change_table :paintings do |t|
      t.references :artist
    end  
  end
end

This doesn't change anything in the database. What am I doing wrong?

2 Answers 2

1

Seems correct .

Did you already run this migration and added this latter ? If yes then create new one OR delete version from schema_migrations .

Way : To add a foreign key column

change_table(:suppliers) do |t|
  t.references :company
end

It creates a company_id(integer) column

To add a polymorphic foreign key column

change_table(:suppliers) do |t|
  t.belongs_to :company, :polymorphic => true
end

It creates company_type(varchar) and company_id(integer) columns .

For further detail refer the link.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this too with a new migration. Still doesn't change the database.
Simply add it like : (add_column :paintings, :artist_id, :integer)
OR (add_column :paintings, :artist_id, :integer ,:references=>"artists")
This worked! Just weird that my original migration didn't work.
1

Try

t.belongs_to :artist

instead

t.references :artist

But your variant should work too, if you test in irb console. Run 'reload' to update.

3 Comments

Please, attach log of your migration
== ReferenceArtistFromPaintings: reverting =================================== == ReferenceArtistFromPaintings: reverted (0.0000s) ========================== == ReferenceArtistFromPaintings: migrating =================================== == ReferenceArtistFromPaintings: migrated (0.0000s) ==========================
This is from the new migration Vik recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.