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:
dbapi_con = session.connection().connection.connection
dbapi_con.execute(str(CreateTable(TempTest.__table__,
bind=session.bind)))
session.add(TempTest(name='foo'))
session.flush() # Succeeds
Is there a nicer way to do this?
Thanks,
Gabor
Gabor Gombas
Morgan Stanley | Enterprise Infrastructure
Lechner Odon fasor 8 | Floor 07
Budapest, 1095
Phone: +36 1 881-4376
[email protected]
Be carbon conscious. Please consider our environment before printing this
email.
--------------------------------------------------------------------------------
NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or
views contained herein are not intended to be, and do not constitute, advice
within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and
Consumer Protection Act. If you have received this communication in error,
please destroy all electronic and paper copies and notify the sender
immediately. Mistransmission is not intended to waive confidentiality or
privilege. Morgan Stanley reserves the right, to the extent permitted under
applicable law, to monitor electronic communications. This message is subject
to terms available at the following link:
http://www.morganstanley.com/disclaimers. If you cannot access these links,
please notify us by reply message and we will send the contents to you. By
messaging with Morgan Stanley you consent to the foregoing.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.