On Nov 22, 2013, at 6:24 AM, Gombas, Gabor <[email protected]> wrote:
> Hi,
>
> How should one create SQLite temporary tables using SQLAlchemy? The following
> does not work:
>
> from sqlalchemy import create_engine, Column, String
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import sessionmaker
> from sqlalchemy.schema import CreateTable
>
> Base = declarative_base()
>
> class TempTest(Base):
> __tablename__ = 'temp_test'
> __table_args__ = {'schema': 'temp'}
> name = Column(String, primary_key=True)
>
> engine = create_engine('sqlite:////tmp/testdb', echo=True)
> Session = sessionmaker(bind=engine)
> session = Session()
>
>
> TempTest.__table__.create(session.bind.execution_options(autocommit=False))
> session.add(TempTest(name='foo'))
> session.flush() # Fails
>
> Although I see the "CREATE TABLE" being emitted, INSERT fails saying the
> table does not exist. If instead of calling TempTest.__table__.create() I use
> the underlying DBAPI connection, then I can make it work, but it's ugly:
you use create() but use the session’s current transaction-bound connection:
TempTest.__table__.create(session.connection())
I’m assuming you’re using @compiles somewhere to get the “TEMPORARY” syntax in
there.
signature.asc
Description: Message signed with OpenPGP using GPGMail
