On Mar 19, 2013, at 6:46 PM, Catherine Devlin <catherine.dev...@gmail.com> wrote:
> 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. OK putting aside Alembic (which is the best approach, if you could evangelize your DBAs to use it...), the most quick and dirty way to get DDL as a string pretty directly is to use the "mock" approach addressed in this FAQ entry: http://www.sqlalchemy.org/trac/wiki/FAQ#HowcanIgettheCREATETABLEDROPTABLEoutputasastring > > 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. So the MockDialect approach is really only appropriate for DDL. For a more generalized way to specifically make any SQL/DDL statement compile to a specific dialect is to pass it to compile(): from sqlalchemy.dialects import postgresql dialect = postgresql.dialect() stmt = my_table.update().values(foo='bar') print(stmt.compile(dialect=dialect)) -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.