Reputation: 845
I am trying to update a row in a table based on some filters. I could get the row_object
and i could update it if the number of rows is one and if more than one it throws StaleDataError
while saving the session.
I have 2 rows in my db
Following is the code..
@require_context
def travel_update(context, travel_id, region, resource):
with write_session() as session:
time_now = timeutils.utcnow()
travel_ref = session.query(models.Region_travel). \
filter_by(id=travel_id, region=region, resource=resource). \
first()
if not travel_ref:
raise exception.idNotFound(travel_id=travel_id)
travel_ref.status = "failed"
travel_ref.updated_at = time_now
travel_ref.save(session)
return travel_ref
Upvotes: 0
Views: 5098
Reputation: 126
I had a similar problem (primarily due to my lack of attention-to-detail). The database table key
was different from the key
defined in my sql_alchemy models (for that table), which lead sql_alchemy to use a column (the old key
) that matched more than one row in the database during the update step of the transaction.
Updating the sql_alchemy model primary key
to the actual db primary key
fixed the problem.
Upvotes: 7