Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test case for values not persisting after add_column #6702

Closed
wants to merge 1 commit into from

Conversation

jdelStrother
Copy link
Contributor

Given a migration like

  puts Post.inspect
  add_column :posts, :body, :string
  Post.find_each do |p|
    p.body = 'foo'
    p.save!
  end

both the assignment to 'body' and the save will succeed, but the value isn't written into the database, and you're left with nil values for 'body'.

(Obviously the "puts Post.inspect" is artificial - in our case, I think a plugin is loading our models prematurely, and so ActiveRecord caches the pre-migration column information)

The attached (failing) test checks that the new column's value is persisted properly. Perhaps this could be achieved by implicitly calling reset_column_information after each schema change. However, I don't care too much about that behaviour - I'd be just as happy with getting NoMethodError on trying to assign to 'body'. We were caught out and lost some production data because the model update appeared to work fine, when it was actually just dropping the values assigned to it.

See #6679 for prior discussion.

Given a migration like

  add_column :posts, :body, :string
  Post.find_each do |p|
    p.body = 'foo'
    p.save!
  end


both the assignment to 'body' and the save will succeed, but the value isn't written into the database, and you're left with nil values for 'body'.

Perhaps add_column should implicitly call reset_column_information?  Having the 'body' accessor raise NoMethodError instead of appearing to succeed would also be fine...
@josevalim
Copy link
Contributor

I think @jonleighton has some plans for not allowing .body to work. Calling reset_column_information would also be possible, since AR already tracks its descendants.

@parndt
Copy link
Contributor

parndt commented Dec 12, 2012

Is it generally advised to assign values in a migration? Given that there's an acceptable workaround of reset_column_information is this an issue that needs solving?

@senny
Copy link
Member

senny commented Mar 7, 2013

I agree with @parndt. This is not necessarily an issue we need to solve. In general using your models in your migrations leads to problems of some sort. If you absolutely have to you can use the workaround.

@rafaelfranca @carlosantoniodasilva thoughts?

@rafaelfranca
Copy link
Member

I agree. Actually I always advice to never change data in migrations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants