简体   繁体   中英

Migration doesn't add column to database

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?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM