rwilliams
rwilliams

Reputation: 21497

Migration fails 'method not found' for added database field

I'm running a big set of migrations on my rails 6 app. When i go through the migrations it always fails when trying to access an specific attribute of a newly added field from the previous migration step. After it fails i simply re-run the migration and it works, apparently it can find the attribute the second time around. Any thoughts on what might be happening here?

deleted_customer = Customer.new(first_name:"Deleted",last_name:"Deleted",active:false,customer_type_id:CustomerType.first.id)
deleted_customer.save(validate:false)

StandardError: An error has occurred, this and all later migrations canceled: unknown attribute 'active' for Customer.

From the migration just before the one that fails

add_column :customers, :active, :boolean, default: true

Am I missing something?

Upvotes: 2

Views: 144

Answers (1)

Vasfed
Vasfed

Reputation: 18454

Rails reads schema information on first access to model and caches it (so that it does not have to read db schema each time you access your model).

You can reset that cache:

Customer.connection.schema_cache.clear!
Customer.reset_column_information

Upvotes: 2

Related Questions