0

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:

  1. SQLAlchemy printing raw SQL from create()
  2. 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.

Ilja Everilä
  • 40,618
  • 6
  • 82
  • 94
mad_
  • 7,475
  • 1
  • 18
  • 32

0 Answers0