Comment 5 for bug 1021023

Revision history for this message
Aaron Rosen (arosen) wrote :

Hi Jason,

Here is a patch that fixes this though it causes most of the unit tests to break because they don't respect foreign key reference using sqlite.

diff --git a/quantum/db/api.py b/quantum/db/api.py
index e10c7d4..0246a95 100644
--- a/quantum/db/api.py
+++ b/quantum/db/api.py
@@ -23,6 +23,7 @@ import time
 import sqlalchemy as sql
 from sqlalchemy import create_engine
 from sqlalchemy.exc import DisconnectionError
+from sqlalchemy.interfaces import PoolListener
 from sqlalchemy.orm import sessionmaker, exc

 from quantum.db import model_base
@@ -56,6 +57,12 @@ class MySQLPingListener(object):
                 raise

+class SqliteForeignKeysListener(PoolListener):
+ """Foreign keys are not enforced in sqlite unless specified."""
+ def connect(self, dbapi_con, con_record):
+ dbapi_con.execute('pragma foreign_keys=ON')
+
+
 def configure_db(options):
     """
     Establish the database, create an engine if needed, and
@@ -75,6 +82,9 @@ def configure_db(options):
         if 'mysql' in connection_dict.drivername:
             engine_args['listeners'] = [MySQLPingListener()]

+ if 'sqlite' in connection_dict.drivername:
+ engine_args['listeners'] = [SqliteForeignKeysListener()]
+
         _ENGINE = create_engine(options['sql_connection'], **engine_args)
         base = options.get('base', BASE)
         if not register_models(base):