SQLAlchemy printing raw SQL from create()

后端 未结 6 2024
别跟我提以往
别跟我提以往 2020-11-30 00:05

I am giving Pylons a try with SQLAlchemy, and I love it, there is just one thing, is it possible to print out the raw SQL CREATE TABLE data generated from

6条回答
  •  无人及你
    2020-11-30 00:12

    I needed to get the raw table sql in order to setup tests for some existing models. Here's a successful unit test that I created for SQLAlchemy 0.7.4 based on Antoine's answer as proof of concept:

    from sqlalchemy import create_engine
    from sqlalchemy.schema import CreateTable
    from model import Foo
    
    sql_url = "sqlite:///:memory:"    
    db_engine = create_engine(sql_url)
    
    table_sql = CreateTable(Foo.table).compile(db_engine)
    self.assertTrue("CREATE TABLE foos" in str(table_sql))
    

提交回复
热议问题