How do I get alembic to emit custom DDL on after_create?

后端 未结 2 1405
无人共我
无人共我 2020-12-30 06:26

I\'ve got a couple of custom DDL statements that I want to run after create table:

update_function = DDL(\"\"\"                                                       


        
2条回答
  •  甜味超标
    2020-12-30 06:52

    Expanding on @zzzeek's answer, this helper works for me:

    from sqlalchemy import Table
    from sqlalchemy.event import listen
    from functools import partial
    
    def on_table_create(class_, ddl):
    
        def listener(tablename, ddl, table, bind, **kw):
            if table.name == tablename:
                ddl(table, bind, **kw)
    
        listen(Table,
               'after_create',
               partial(listener, class_.__table__.name, ddl))
    

    Then you'd do:

    on_table_create(Session, update_function)
    on_table_create(Session, update_trigger)
    

提交回复
热议问题