DDL¶
Because of some of the limitations in the SQLAlchemy API, it’s not possible to
asynchronously create tables using sqlalchemy.schema.Table.create()
or
sqlalchemy.schema.MetaData.create_all()
.
Instead of:
users = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
)
users.create(engine)
you can use sqlalchemy.schema.CreateTable
or
AsyncEngine.run_in_thread()
:
await engine.execute(CreateTable(users))
await engine.run_in_thread(users.create, engine.sync_engine)
For MetaData.create_all()
,
instead of:
metadata.create_all(engine)
you have to do:
await engine.run_in_thread(metadata.create_all, engine.sync_engine)