On Jan 26, 2011, at 6:32 PM, A.M. wrote:

> Well, I spoke too soon :( What is the mistake in the following sample code 
> which causes the COMMITs to be emitted? Setting autocommit to either True or 
> False emits the same SQL. I think this is a case of staring at the same code 
> too long causing brain damage- thanks for your patience and help!
> 
> from sqlalchemy.engine import create_engine
> from sqlalchemy.orm.session import sessionmaker
> from sqlalchemy.orm import scoped_session
> from sqlalchemy.schema import DDL,MetaData,Table
> 
> engine = create_engine('postgresql://localhost/test',echo=True)
> session = scoped_session(sessionmaker(bind=engine))
> metadata = MetaData()
> metadata.bind = engine
> 
> Table('test1',metadata)
> Table('test2',metadata)
> metadata.create_all()

metadata.create_all() looks at the "bind" attribute, then uses it to execute 
each DDL statement.   The "bind" here is an engine so it uses connectionless 
execution.  connectionless execution is usually autocommit as documented here:  
http://www.sqlalchemy.org/docs/core/connections.html#connectionless-execution-implicit-execution

to emit create_all() in a transaction:

conn = engine.connect()
with conn.begin():
        metadata.create_all(conn)



> 
> 2011-01-26 18:27:48,284 INFO sqlalchemy.engine.base.Engine.0x...6cd0 select 
> version()
> 2011-01-26 18:27:48,284 INFO sqlalchemy.engine.base.Engine.0x...6cd0 {}
> 2011-01-26 18:27:48,286 INFO sqlalchemy.engine.base.Engine.0x...6cd0 select 
> current_schema()
> 2011-01-26 18:27:48,286 INFO sqlalchemy.engine.base.Engine.0x...6cd0 {}
> 2011-01-26 18:27:48,288 INFO sqlalchemy.engine.base.Engine.0x...6cd0 select 
> relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where 
> n.nspname=current_schema() and lower(relname)=%(name)s
> 2011-01-26 18:27:48,288 INFO sqlalchemy.engine.base.Engine.0x...6cd0 {'name': 
> u'test1'}
> 2011-01-26 18:27:48,290 INFO sqlalchemy.engine.base.Engine.0x...6cd0 select 
> relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where 
> n.nspname=current_schema() and lower(relname)=%(name)s
> 2011-01-26 18:27:48,290 INFO sqlalchemy.engine.base.Engine.0x...6cd0 {'name': 
> u'test2'}
> 2011-01-26 18:27:48,291 INFO sqlalchemy.engine.base.Engine.0x...6cd0 
> CREATE TABLE test1 (
> )
> 
> 
> 2011-01-26 18:27:48,291 INFO sqlalchemy.engine.base.Engine.0x...6cd0 {}
> 2011-01-26 18:27:48,292 INFO sqlalchemy.engine.base.Engine.0x...6cd0 COMMIT
> 2011-01-26 18:27:48,293 INFO sqlalchemy.engine.base.Engine.0x...6cd0 
> CREATE TABLE test2 (
> )
> 
> 
> 2011-01-26 18:27:48,293 INFO sqlalchemy.engine.base.Engine.0x...6cd0 {}
> 2011-01-26 18:27:48,294 INFO sqlalchemy.engine.base.Engine.0x...6cd0 COMMIT
> 
> -- 
> 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.
> 

-- 
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.

Reply via email to