> On 26 Aug 2016, at 21:42, adaptable (Metaframework) <rognoni....@gmail.com> > wrote: > > Hi all, > this comment on Github is well formatted: > > https://github.com/zzzeek/sqlalchemy/pull/295#issuecomment-242834334 > > I see the wrong todo.id after the commit (a log string instead 1) > > [...] > 2016-08-26 21:36:56,071 INFO sqlalchemy.engine.base.Engine COMMIT > ######### before commit - id: None > 2016-08-26 21:36:56,073 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) > 2016-08-26 21:36:56,073 INFO sqlalchemy.engine.base.Engine INSERT INTO todo > (description, done, user_id) VALUES (?, ?, ?) > 2016-08-26 21:36:56,073 INFO sqlalchemy.engine.base.Engine ('TODO test', > None, None) > 2016-08-26 21:36:56,074 INFO sqlalchemy.engine.base.Engine COMMIT > ######### after commit - id: 2016-08-26 21:36:56,075 INFO > sqlalchemy.engine.base.Engine BEGIN (implicit) > 2016-08-26 21:36:56,076 INFO sqlalchemy.engine.base.Engine SELECT todo.id AS > todo_id, todo.description AS todo_description, todo.done AS todo_done, > todo.user_id AS todo_user_id > FROM todo > WHERE todo.id = ? > 2016-08-26 21:36:56,076 INFO sqlalchemy.engine.base.Engine (1,) > 1 > 2016-08-26 21:36:56,077 INFO sqlalchemy.engine.base.Engine SELECT todo.id AS > todo_id, todo.description AS todo_description, todo.done AS todo_done, > todo.user_id AS todo_user_id > FROM todo > 2016-08-26 21:36:56,077 INFO sqlalchemy.engine.base.Engine () > 1 TODO test > > running this code on MAC (Python 2.7.10) with SQLAlchemy version 1.0.14 > > https://github.com/rognoni/adaptable/blob/master/Backend/Python-Flask/storage/sqlalchemy_sqlite.py > > [...] > ### test > session = db_schemas.examples.session() > todo = db_schemas.examples.classes.todo(description="TODO test") > session.add(todo) > print "######### before commit - id: ", todo.id > session.commit() > print "######### after commit - id: ", todo.id > > for instance in session.query(db_schemas.examples.classes.todo): > print instance.id, instance.description > > Using the query the instance.id is correct: 1
I think you are misunderstanding the output. The statement you are executing is: > print "######### after commit - id: ", todo.id Python is printing out the first part before it even tries to evaluate “todo.id”. Then, when it goes to evaluate it, a database query is triggered (because all the attributes of todo were expired after the commit). The query output is logged to stdout. Then finally the print statement prints the value of todo.id, which is the “1” on its own 6 lines further down. Here’s a non-SQLAlchemy example demonstrating the same thing: >>> class A(object): ... @property ... def attribute(self): ... print 'some log output' ... return 'actual value' ... >>> a = A() >>> print 'the value is:', a.attribute the value is: some log output actual value >>> print 'using interpolation, the value is %s' % a.attribute some log output using interpolation, the value is actual value When you use string interpolation, “a.attribute” has to be evaluated and substituted into the string before the print statement can run. If you use the print *function* rather than the print *statement*, all the arguments will be evaluated before anything is displayed, so this also wouldn’t happen. Hope that helps, Simon -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.