On Jun 12, 2011, at 5:40 AM, Vinay Sajip wrote: > The following short script behaves differently if run with no arguments or > with > arguments: > metadata.bind = "sqlite:////tmp/schools.db"
the Engine is associated with the MetaData. The echo flag is off. > setup_all(create_tables=True) > school = School(name='Rydell High') > session.commit() The Session begins operations on the database, invoking engine.connect() on the underlying Engine. The Connection object checks the "echo" flag of the Engine to determine if logging should occur for subsequent calls to execute(). As you're aware, we try to minimize how often we call upon logging to check for enabled - the execute() method of Connection is a critical performance point, so whatever echo was at time of Connection start is what will be used. > session.commit() Still in the commit. The transaction is committed the Connection closed, and all data within the Session is expired. > r = Subject._descriptor.find_relationship('teachers') > t = r.table > if len(sys.argv) > 1: > c = Subject.school == school > else: > c = Subject.school_id == school.id > s = select([Teacher.name], > and_(c, t.c.prof_id == Teacher.id, t.c.course_id == Subject.id)) An expression is created, comparing "Subject.school_id" to a value. In the case of passing the School object, the value is a bind which will invoke a callable upon the School at statement compilation time to retrieve the value of ".id", which remember has been expired due to the commit(). In the case of passing school.id, the Session is told to retrieve the expired value immediately, so the Session wakes up, gets a Connection, checks the bind flag again, retrieves the formerly expired "school" row. > metadata.bind.echo = True the echo flag is turned on which only affects subsequent Connection objects. In one case, the Session hasn't done anything yet, in the other, it's started up a transaction with a new Connection already. > print('Executing query using SQLAlchemy', __version__) > session.execute(s) In the first case, the lazy callable inside the comparison expression loads the "school.id" value, which wakes up the Session with a new Connection, using the new echo flag. In the second case, this does not happen. Also see the note at the bottom of http://www.sqlalchemy.org/docs/core/engines.html?highlight=echo#configuring-logging . -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.