Skip to content Skip to sidebar Skip to footer

Sqlalchemy Expression Language And Sqlite's On Delete Cascade

I have two related tables namely users and roles and the relationship between them is many-to-many, so another association table userroles also exists. The userroles table keeps tr

Solution 1:

Okay it seems like you need to enforce foreign keys for sqlite. So based on this answer one should do:

from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlite3 import Connection as SQLite3Connection

@event.listens_for(Engine, "connect")
def _set_sqlite_pragma(dbapi_connection, connection_record):
    if isinstance(dbapi_connection, SQLite3Connection):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON;")
        cursor.close()

Post a Comment for "Sqlalchemy Expression Language And Sqlite's On Delete Cascade"