On Jan 26, 2011, at 5:45 PM, Michael Bayer wrote:
> 
> 
> From this it follows that if you'd like to emit several DDL statements in a 
> transaction, the usage is no different for DDL expressions than for any other 
> kind of DML statement (i.e insert/update/delete).   Use 
> connection.begin()/transaction.commit() as documented at:
> 
>       http://www.sqlalchemy.org/docs/core/connections.html#using-transactions 
> .    
> 
> autocommit is described right after that:
> 
>       
> http://www.sqlalchemy.org/docs/core/connections.html#understanding-autocommit
> 
> Also the public API for _execution_options is the execution_options() 
> generative call: 
> 
>       
> http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=execution_options#sqlalchemy.sql.expression.Executable.execution_options
>  .

Thanks for the prodding- I figured out my bug. Here is sample code that 
demonstrates a little surprise. 

First, this code that emits:
BEGIN (implicit)
SELECT 1
ROLLBACK

=========================
from sqlalchemy.engine import create_engine
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import scoped_session
from sqlalchemy.schema import DDL

engine = create_engine('postgresql://localhost/test',echo=True)
session = scoped_session(sessionmaker(bind=engine))

DDL("SELECT 1").execute(bind=session)
session.rollback()
=========================

and here is the buggy code which emits:
SELECT 1
COMMIT

from sqlalchemy.engine import create_engine
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import scoped_session
from sqlalchemy.schema import DDL

engine = create_engine('postgresql://localhost/test',echo=True)
session = scoped_session(sessionmaker(bind=engine))

DDL("SELECT 1").execute(bind=engine)
session.rollback()
=========================

Can you spot the difference? The DDL in the first code is bound to the session 
and the latter code mistakenly binds to the engine for execution resulting in 
two different execution paths. In hindsight, I guess it makes sense, but it 
certainly was not easy to find...

Cheers,
M

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