Hi
Newbie here. I think I have a fundamental misunderstanding of either how
session works or you inspect works. My goal is to track changes to my
object. I wrote up this sample to show what I'm having trouble
understanding.
from sqlalchemy import create_engine, inspect
from sqlalchemy import Column, BigInteger, String, Date, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from dateutil.relativedelta import *
Base = declarative_base()
class SaTest(Base):
__tablename__ = 'sa_test'
id = Column(BigInteger, primary_key=True)
name = Column(String(50))
dob = Column(Date)
number_of_children = Column(Integer)
def __repr__(self):
return self.name
engine =
create_engine('mysql://mysqluser:mysqlpassword@servername/databasename')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
"""
# run this the first time to create the data
jim = SaTest(name='Jim', dob=parse('1971-02-23'), number_of_children=3)
session.add(jim)
joe = SaTest(name='Joe', dob=parse('1968-06-09'), number_of_children=5)
session.add(joe)
session.commit()
"""
jim = session.query(SaTest).filter_by(id=1).one()
jim.dob = jim.dob + relativedelta(days=+14)
jim.number_of_children = jim.number_of_children + 1
#joe = session.query(SaTest).filter_by(id=2).first() <---- if I
uncomment this line, inspect doesn't display the changes in history
insp = inspect(jim)
for attr, attr_state in insp.attrs.items():
if attr_state.history.has_changes():
print('{}: {}'.format(attr, attr_state.value))
print('History: {}\n'.format(attr_state.history))
I've built this based on the example at the bottom of page 107 in the
O'Reilly Essential SQLAlchemy book.
It seems to my that running the second query is somehow invalidating the
inspection of the jim object. I'd really like to understand why, but
haven't been able to find anything to help explain it to me.
Any help would really be appreciated.
-Jim
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.