From afbfd40578ca93babe191bb0edef360822c91a27 Mon Sep 17 00:00:00 2001 From: Elrond Date: Fri, 18 Jan 2013 18:42:52 +0100 Subject: [PATCH] Enable foreign key checking on sqlite. sqlite normally does not check foreign key referential integrity. But it can do so. So let's use it. Better safe than sorry. --- mediagoblin/db/open.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mediagoblin/db/open.py b/mediagoblin/db/open.py index d976acd8..5fd5ed03 100644 --- a/mediagoblin/db/open.py +++ b/mediagoblin/db/open.py @@ -15,7 +15,7 @@ # along with this program. If not, see . -from sqlalchemy import create_engine +from sqlalchemy import create_engine, event import logging from mediagoblin.db.base import Base, Session @@ -66,9 +66,20 @@ def load_models(app_config): exc)) +def _sqlite_fk_pragma_on_connect(dbapi_con, con_record): + """Enable foreign key checking on each new sqlite connection""" + dbapi_con.execute('pragma foreign_keys=on') + + def setup_connection_and_db_from_config(app_config): engine = create_engine(app_config['sql_engine']) + + # Enable foreign key checking for sqlite + if app_config['sql_engine'].startswith('sqlite://'): + event.listen(engine, 'connect', _sqlite_fk_pragma_on_connect) + # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) + Session.configure(bind=engine) return DatabaseMaster(engine) -- 2.25.1