Turn on foreign key enforcement for sqlite connections.
By default, for some legacy reasons, sqlite databases don't actually
pay attention to foreign key constraints. This will ensure every sqlite
connection turns on foreign key constraints.
Source code in tigrqc/extensions.py
| Python |
|---|
| def register_sqlite_foreign_key_enforcement(
app: Flask, app_db: SQLAlchemy
) -> None:
"""Turn on foreign key enforcement for sqlite connections.
By default, for some legacy reasons, sqlite databases don't actually
pay attention to foreign key constraints. This will ensure every sqlite
connection turns on foreign key constraints.
"""
with app.app_context():
if app_db.engine.dialect.name != 'sqlite':
return
@event.listens_for(app_db.engine, 'connect')
def set_sqlite_foreign_keys_on(dbapi_connection, _):
cursor = dbapi_connection.cursor()
cursor.execute('PRAGMA foreign_keys=ON')
cursor.close()
|