Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created August 15, 2018 18:26
Show Gist options
  • Select an option

  • Save doobeh/a68f375c6cccbfa3a06da7894c0fdecc to your computer and use it in GitHub Desktop.

Select an option

Save doobeh/a68f375c6cccbfa3a06da7894c0fdecc to your computer and use it in GitHub Desktop.
Why when the 110 value is set, doesn't the `get_history` see 100 as the deleted value?
def price_logger(mapper, connection, target):
data = target.__dict__.copy()
print('Update event caught')
print("Current Value:", data["retail"]) # Current value (working as expected)
value = get_history(target, 'retail')
print(f'Old value was {value}, new is {data["retail"]}')
print('-'*10)
class History(db.Model):
id = db.Column(db.Integer, primary_key=True)
gtin = db.Column(db.String(15), nullable=False)
retail = db.Column(db.Integer)
created = db.Column(db.DateTime, default=datetime.utcnow)
event.listen(Item, 'after_update', price_logger)
def __init__(self, gtin, retail):
self.gtin = gtin
self.retail = retail
def __repr__(self):
return f"{self.gtin} : {self.retail} : {self.created}"
Update event caught
Current Value: 44
Old value was History(added=(), unchanged=[44], deleted=()), new is 44
----------
.Update event caught
Current Value: 100
Old value was History(added=[100], unchanged=(), deleted=[44]), new is 100
----------
Update event caught
Current Value: 110
Old value was History(added=[110], unchanged=(), deleted=()), new is 110
db.session.add(Item(**example_item))
db.session.commit()
item = Item.query.first()
item.retail = 100
db.session.commit()
item.retail = 110
db.session.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment