create() and create_all() take a "bind" argument which can be an  
engine or connection.  you want the connection in this case.

On Feb 5, 2009, at 5:27 AM, Chris Miles wrote:

>
> I notice that a table create (and drop/etc) is always followed by an
> implicit commit.  Is it possible to suppress the commit or force SA to
> create multiple tables in one transaction so that if any fail they can
> all be rolled back?
>
> Here's some code to demonstrate what I want.  In this example, the
> table creation fails half way through, but leaves 2 tables created.  I
> want the CREATEs rolled back if any of them fail.
>
>
> import sqlalchemy as sa
> engine = sa.create_engine('sqlite:///test1.sqlite')
> engine.echo = True
> metadata = sa.MetaData()
>
> table1 = sa.Table("table1", metadata,
>    sa.Column('col1', sa.types.Integer,),
> )
> table2 = sa.Table("table2", metadata,
>    sa.Column('col1', sa.types.Integer,),
> )
> table3 = sa.Table("table3", metadata,
>    sa.Column('col1', sa.types.Integer,),
> )
>
> metadata.bind = engine
> connection = engine.connect()
> trans = connection.begin()
> try:
>    table1.create()
>    table2.create()
>    table2.create()
>    table3.create()
>    trans.commit()
> except:
>    trans.rollback()
>
>
> I can't find away to link table.create() with the existing
> transaction.
>
> Note: I don't want to just use create_all(). This is part of an in-
> house schema version control system.
>
> Note 2: If I create tables from SQL (sending "CREATE TABLE ..."
> strings) within the transaction then I get the desired behaviour.
> However, I'd prefer to use table.create() for convenience (i.e. across
> multiple engines).
>
> Cheers,
> Chris Miles
>
>
> >


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