I wish to create scripts containing the sql statements executed by sqlalchemy during table/schema creations. I DO NOT want to print a single raw query that I can do using print(statement.compile(someengine))
but want all the queries to be saved as a script. I know there is a flag called echo=True
which will log all the queries but it will still be a log and not a script.
After doing some of the googling I landed up on below two links:
- SQLAlchemy printing raw SQL from create()
- http://docs.sqlalchemy.org/en/latest/faq/metadata_schema.html#how-can-i-get-the-create-table-drop-table-output-as-a-string
The documentation link looks promising but throwing me an error Example code which I tried:
from sqlalchemy import create_engine
uri ='postgresql://user:password@hostname/database_name'
def dump(sql, *multiparams, **params):
print(sql.compile(dialect=engine.dialect))
engine = create_engine(uri, strategy='mock', executor=dump)
Above code is throwing me this error
'MockConnection' object has no attribute 'url'
Any help or suggestion would be appreciated.