db:migrate unknown attribute on insert via migration

286 views
Skip to first unread message

t...@stinto.com

unread,
Mar 27, 2019, 10:36:20 AM3/27/19
to Ruby on Rails: Talk
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.

Eito Katagiri

unread,
Mar 27, 2019, 5:33:42 PM3/27/19
to rubyonra...@googlegroups.com
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 <t...@stinto.com>:
--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@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.

t...@stinto.com

unread,
Mar 28, 2019, 3:42:27 AM3/28/19
to Ruby on Rails: Talk
Thanks for the links - that clears it up.

The reason for using a migration and not seed data, is that this is real data needed in our production db, and I use seeds.rb to populate my development db with data to test with.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages