You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Chaining migrations fails when accessing the same model over again #30
Just happened to encounter a problem when running multiple migrations in a row. (not sure enough, but possibly related with Rails' caching behavior on column information).
Sorry I haven't had enough time to dig this any further until now (am I totally missing something here, or is there any known solutions already?), but just thought it would be worthwhile to raise it.
How to reproduce
0) Create a target model (Product here).
This model could be created separately in advance.
1) Within data method, access Product.
It can be anything that touches the model (e.g. just doing p = Product.where(id: 1) )
classCreateProducts < ActiveRecord::Migration[5.1]defchangecreate_table:productsdo |t|
endenddefdata# do whatever operation that requires access to Product modelProduct.create!endend
2) Run another migration and change Product instance.
classAddName < ActiveRecord::Migration[5.1]defchangeadd_column:products,:name,:stringenddefdata# calling a instance method for Product (not a class method like update_all )product=Product.find(1)product.update!(name: 'foobar')endend
This happens only when you run two migrations (1 and 2) all together.
workaround
The quick (and obvious) solution on this would be running the migration one by one, which at times is not practical in some situations where you often have to catch up with the large diff in one run.
Another approach I occasionally found is to insert Product.reset_column_information before the (2) second procedure starts.
Just wanted to know if there is any solutions with this.
At minimum, adding a few caution on README would save someone's life :)
Hi @fursich This workaround is a good way to go in your case. The same things would happen in a basic migration and Rails developer should aware of that because it's already documented in Rails docs.
Activity
ka8725 commentedon Oct 3, 2017
Hi @fursich This workaround is a good way to go in your case. The same things would happen in a basic migration and Rails developer should aware of that because it's already documented in Rails docs.
ka8725 commentedon Oct 3, 2017
Anyway improved README. Thanks for your contribution @fursich !
fursich commentedon Oct 4, 2017
Hi @ka8725, thanks a lot for your swift answer. Looks like I had had a total misconception that that caching part is also taken care of by the gem.
A simple notion on README would be really great. Thanks really again!