Ruby on Rails Wednesday, March 27, 2019

You need to call .reset_column_information in third migration. Please refer 

You can insert and update data in migration, but you might want to consider using db/seeds.rb instead. 

HTH

2019年3月27日(水) 23:36 <tw@stinto.com>:
I'm running Rails 5.0.6, and I can't Google myself to any other occurences of this problem.

The migrations fail when I have a migration that inserts data into a table, that has data for a column that was added in a previous migration file executed by the same migrate command 

Basically I have 3 migrations that are run with the following command
rails db:migrate


first migration chronologically: 

create_table :campaign_orders do |t|
  t
.integer :order
end


second migration:

add_column :campaign_orders, :priority, :integer


third migration:

CampaignOrder.create(order: 0, priority: 1)


The third migration fails, if all migrations are outstanding when running migrate command, but succeeds if you run the migrate command again.  It fails with the following error:
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
unknown attribute 'priority' for CampaignOrder.
/Users/tomwilliams/Projects/incard-rails/db/migrate/20190326143614_insert_accepted_email_campaign.rb:66:in `up'
/Users/tomwilliams/Projects/incard-rails/bin/rails:9:in `require'
/Users/tomwilliams/Projects/incard-rails/bin/rails:9:in `<top (required)>'
/Users/tomwilliams/Projects/incard-rails/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Caused by:
ActiveModel::UnknownAttributeError: unknown attribute 'priority' for CampaignOrder.
/Users/tomwilliams/Projects/incard-rails/db/migrate/20190326143614_insert_accepted_email_campaign.rb:66:in `up'
/Users/tomwilliams/Projects/incard-rails/bin/rails:9:in `require'
/Users/tomwilliams/Projects/incard-rails/bin/rails:9:in `<top (required)>'
/Users/tomwilliams/Projects/incard-rails/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Now, I can just "fix" it by running the migrate command again, or as I did, rollback the migrations and put the added column in the create table migration and deleting the add_column migration.

My question is - does anybody know if this is a known issue, and if so, could someone provide a link.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1eb04bc6-7c56-4555-a90e-47b18a2f8d60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CA%2BfENYVPaY3Q-EAJEVB96M%2B9tfGAEDE6NJaOQrtzK9g6chmCWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment