Hi! I'm looking for a way to capture the text of SQL that SQLAlchemy is
prepared to execute, without actually executing the SQL. There has to be a
way, right?
Why (#1): I'm working on a DDL-generating tool for DBAs who no-way-no-how
will let some Python tool change their database directly.
Why (#2): I can grab the SQL of most of the statements I want to run with
str() calls, like str(my_table.update().values(foo='bar')) and
str(sqlalchemy.schema.CreateTable(new_table)). But this doesn't generate
the same SQL that actually executing does; specifically, it doesn't create
the PK as SERIAL in postgres.
My code:
import sqlalchemy
connection_string = 'postgresql://user:pw@localhost/db'
engine = sqlalchemy.create_engine(connection_string, echo=True)
metadata = sqlalchemy.MetaData(bind=engine)
new_table = sqlalchemy.Table('new_table', metadata,
sqlalchemy.Column('id',
sqlalchemy.types.Integer,
primary_key = True,
autoincrement = True))
print(sqlalchemy.schema.CreateTable(new_table))
print('\n\n---------\n\n')
metadata.create_all(engine)
Result:
CREATE TABLE new_table (
id INTEGER NOT NULL,
PRIMARY KEY (id)
)
---------
2013-03-19 21:12:45,248 INFO sqlalchemy.engine.base.Engine
CREATE TABLE new_table (
id SERIAL NOT NULL,
PRIMARY KEY (id)
)
As far as I can tell, Alembic isn't quite right for this task; I'm writing
for users who won't want to set up an alembic directory with named
revisions and so forth. But something analogous to Alembic's "offline
mode" is exactly what I'm looking for for SQLAlchemy.
Thanks,
--
- Catherine
http://catherinedevlin.blogspot.com
--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.