简体   繁体   中英

What did I do wrong with my Rails migrations for Heroku?

I'm trying to add my Address table's text property as the address property of my Brewery table, then delete the Address table. I'm doing this on Heroku after a git push heroku master by running heroku run rake db:migrate .

My first migration which creates the new field for the data works fine. But my second migration fails immediately- it says it can't find the Address table. My third migration never runs, but it's the one that was supposed to delete the Address table to tidy everything up.

What am I doing wrong? These migrations worked on my local box, but are failing on Heroku:

1:

class AddAddressStringToBrewery < ActiveRecord::Migration
  def change
    add_column :breweries, :address, :string
  end
end

2:

class MoveAddressToString < ActiveRecord::Migration
  def change
    Address.all.each do |address|
      brewery = address.brewery
      brewery.update(address: address.text)
    end
  end
end

3:

class DropAddressTable < ActiveRecord::Migration
  def change
    drop_table :addresses
  end
end

My best guess is that Heroku is reading my schema and dropping the table, before running my migrations. I could always stage these migrations in individual git commits to force it to work, but I'd really like to know where I went wrong.

Thanks!

It's because the file names of your migrations are in the wrong order. Rails will migrate files based on the timestamp of the file name. So if you want #2 to run before #3, you have to rename it to a file name timestamp that comes before #3.

Also, you don't show the migration which creates the Address table. The table may not be in your production database.

尝试用heroku run rake db:migrate来更新生产数据库。

I realized what I did wrong. My push to Heroku with these migrations also deleted my Address model. When I ran them in my local environment it was before I cleaned up the old Address code. I added the Address model back long enough to run the migrations and they all run fine.

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