SqlAlchemy: How to implement DROP TABLE … CASCADE?

前端 未结 2 2111
长情又很酷
长情又很酷 2020-12-31 05:58

I need to drop tables in a PostgreSQL database that have foreign key constraints and require DROP TABLE ... CASCADE.

I could execute raw SQL: engi

2条回答
  •  佛祖请我去吃肉
    2020-12-31 06:53

    You can customize the compilation of constructs like this:

    from sqlalchemy.schema import DropTable
    from sqlalchemy.ext.compiler import compiles
    
    @compiles(DropTable, "postgresql")
    def _compile_drop_table(element, compiler, **kwargs):
        return compiler.visit_drop_table(element) + " CASCADE"
    

    This appends CASCADE to the DROP TABLE statement issued for the postgresql dialect while keeping all other dialects the same.

提交回复
热议问题