The same record different instances save() behavior #89

Closed
coderbuzz opened this Issue May 25, 2012 · 1 comment

Comments

Projects
None yet
2 participants

Please considering a bug:
I'm in situation which fetching the same db record but different instance, each modified somewhere.

    item1 = get_item(filter='A') # return new instance of rec A
    item2 = get_item(filter='A') # also return new instance of rec A

    # item1 and item2 refer to the same db record, but different instance

    item1.some_field = 'some value'
    item1.save()

    item2.other_field = 'other value'
    item2.save()

The item1 data won't saved correctly after committing transaction (different field changes)

Owner

coleifer commented May 25, 2012

This is correct, calling a model's save method will wholesale update the row in the database with the field data present on the model instance at the time of saving. item2 will not automatically receive changes made to item1, since they are separate instances. This is intended behavior.

If you want to only update a single column, use update:

ItemModel.update(other_field=item2.other_field).where(id=item2.id).execute()

coleifer closed this May 25, 2012

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