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
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.