[Sqlalchemy-tickets] Issue #3522: Attribute history not tracked when using session.merge() (zzzeek/
Brought to you by:
zzzeek
|
From: Sylwester K. <iss...@bi...> - 2015-09-02 19:30:19
|
New issue 3522: Attribute history not tracked when using session.merge() https://bitbucket.org/zzzeek/sqlalchemy/issues/3522/attribute-history-not-tracked-when-using Sylwester Kardziejonek: I've noticed that the attribute history of my objects is not tracked. The object comes from session.merge() like it was just freshly retrieved from database. ``` #!python shipping_id = int(request.matchdict.get('id')) # Get model from db db_shipping = db.session.query(m.Shipping).get(shipping_id) # Create updated instance updated_shipping = m.Shipping(id=shipping_id, price=999) # Merge it with session merged = db.session.merge(updated_shipping) # I've noticed an actual UPDATE is emitted at this point # Attributes values are correctly reflected, but the history is all 'unchanged' # Simply iterates over history of each attribute changeset(merged) # History is all 'unchanged' ``` >From the other hand, if I add one line, this works: ``` #!python shipping_id = int(request.matchdict.get('id')) # Get model from db db_shipping = db.session.query(m.Shipping).get(shipping_id) # When I add this, the history is tracked properly changeset(db_shipping) # Create updated instance updated_shipping = m.Shipping(id=shipping_id, price=999) # Merge it with session merged = db.session.merge(updated_shipping) # Attributes values are correctly reflected, history state is correct # Simply iterates over history of each attribute changeset(merged) ``` ``` #!python def changeset(obj): data = {} for prop in obj.__mapper__.iterate_properties: history = get_history(obj, prop.key) if history.has_changes(): old_value = history.deleted[0] if history.deleted else None new_value = history.added[0] if history.added else None if new_value: data[prop.key] = [new_value, old_value] return data ``` |