Session.query(MyModel).filter_by(foo=old_foo_value).update({'foo': 
new_foo_value})

This generates something like this: UPDATE mymodel SET foo=%s WHERE 
mymodel.foo=%s

If I read that correctly it means that the update won't set any rows if the 
value foo has changed in some other transaction since I last read it, so I 
won't end up overwriting anything that's been changed by someone else.

If I use .all() and make my changes on the instances, the generated sql 
issues will update where the primary key matches, rather than 'foo': UPDATE 
mymodel SET foo=%s WHERE mymodel.uuid=%s


On Thursday, April 24, 2014 2:03:19 AM UTC+1, Michael Bayer wrote:
>
> perhaps I’m missing something but wouldn’t you just change the update() 
> here to all(), so that you SELECT only those rows you care about into 
> memory, then change each “foo” as needed and flush?   I’m not seeing what 
> the issue is.  The row isn’t locked if you aren’t using SELECT..FOR UPDATE.
>
>
> On Apr 23, 2014, at 8:27 PM, Tim Kersten <[email protected] <javascript:>> 
> wrote:
>
> I'd like to run a data migration on a live server, but only update rows if 
> the data hasn't changed since I've read it, and would like to do so 
> optimistically, so without locking the row. Doing so like below works and 
> prevents me updating rows that have changed since I last read the row.
>
> Session.query(MyModel).filter_by(foo=old_foo_value).filter_by(bar=old_bar_value).update({'foo':
>  
> new_foo_value})
>
> While the approach works, it doesn't use the ORM. (I use an after_flush 
> hook to inspect & log changes from dirty instances in the session).  
>
> Is there a way to update an ORM instance conditionally like above?
>
> Kindest Regards,
> Tim
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to