Get information from Session's add/commit #11429
-
|
Hey guys, Reaching out hoping to pick your brains and hear your thoughts on a blocker I'm struggling with. I have a small app that's periodically performing a bunch of operations, on a MySQL/MariaDB database, and the result of some of the operations dictates the flow of the rest of the application. I have two models that are related to each other: The flow of application is as it goes:
The update of values happens via the however, neither of these return any information about the number of rows matched or updated. I want to be able to know if, for example, changing the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
so the unit of work checks the number of rows matched to ensure it matches how many objects it worked on in the first place. so if you are confirming rows matched (as opposed to "definitely changed the value of is_alive"), the UOW does that for you since it is updating on primary key and will error out if any of those primary keys are not present. that is, if you are looking for confirmation that First one, to do this from a UOW perspective you want to SELECT..FOR UPDATE your rows first, take a look at what all_parents = session.scalars(select(Parent).where(Parent.is_alive).with_for_update()).all()
for parent in all_parents:
assert parent.is_alive
parent.is_alive = False
session.commit()that would do what you're looking for, confirm is_alive changed. with_for_update() will lock the rows that were SELECTed. The other way is you can use an UPDATE statement directly as documented here. you can use this with the SELECT...FOR UPDATE approach above if you want to lock the rows up front. Or you could set criteria in the update itself, to only match rows where "is_alive" is True and set them to False; in that case the matched rowcount will be the same as the updated rowcount: result = session.execute(update(Parent).where(Parent.is_alive).values(is_alive=False))
assert resuilt.rowcount == <expected number> |
Beta Was this translation helpful? Give feedback.
so the unit of work checks the number of rows matched to ensure it matches how many objects it worked on in the first place. so if you are confirming rows matched (as opposed to "definitely changed the value of is_alive"), the UOW does that for you since it is updating on primary key and will error out if any of those primary keys are not present. that is,
len(all_parents)is the number of rows matched if the commit succeeds.if you are looking for confirmation that
is_alivewas different first, then changed value, while MySQL does support an UPDATE mode that returns this for an UPDATE statement, that is, will give you number of rows changed from an update, SQLAlchemy turns that mode off …