views:

635

answers:

3

The new version of SQLite has the ability to enforce Foreign Key constraints, but for the sake of backwards-compatibility, you have to turn it on for each database connection separately!

sqlite> PRAGMA foreign_keys = ON;

I am using SQLAlchemy -- how can I make sure this always gets turned on? What I have tried is this:

engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=True)
engine.execute('pragma foreign_keys=on')

...but it is not working!...What am I missing?

EDIT: I think my real problem is that I have more than one version of SQLite installed, and Python is not using the latest one!

>>> import sqlite3
>>> print sqlite3.sqlite_version
3.3.4

But I just downloaded 3.6.23 and put the exe in my project directory! How can I figure out which .exe it's using, and change it?

+3  A: 

I now have this working:

Download the latest sqlite and pysqlite2 builds as described above: make sure correct versions are being used at runtime by python.

import sqlite3   
import pysqlite2 
print sqlite3.sqlite_version   # should be 3.6.23.1
print pysqlite2.__path__       # eg C:\\Python26\\lib\\site-packages\\pysqlite2

Next add a PoolListener:

from sqlalchemy.interfaces import PoolListener
class ForeignKeysListener(PoolListener):
    def connect(self, dbapi_con, con_record):
        db_cursor = dbapi_con.execute('pragma foreign_keys=ON')

engine = create_engine(database_url, listeners=[ForeignKeysListener()])

Then be careful how you test if foreign keys are working: I had some confusion here. When using sqlalchemy ORM to add() things my import code was implicitly handling the relation hookups so could never fail. Adding 'nullable=False' to some ForeignKey() statements helped me here.

The way I test sqlalchemy sqlite foreign key support is enabled is to do a manual insert from a declarative ORM class:

# example
ins = Coverage.__table__.insert().values(id = 99,
                                    description = 'Wrong',
                                    area = 42.0,
                                    wall_id = 99,  # invalid fkey id
                                    type_id = 99)  # invalid fkey_id
session.execute(ins) 

Here 'wall_id' and 'type_id' are both ForeignKey()'s and sqlite throws an exception correctly now if trying to hookup invalid fkeys. So it works! If you remove the listener then sqlalchemy will happily add invalid entries.

I believe the main problem may be multiple sqlite3.dll's (or .so) lying around.

CarlS
Did you use the PRAGMA the same way I have done?
Nick Perkins
Thanks, I got it working too.Indeed, the problem was multiple copies of SQLite on my machine...fixing that, and using the PoolListener have worked perfectly!
Nick Perkins
+2  A: 

I had the same problem before (scripts with foreign keys constraints were going through but actuall constraints were not enforced by the sqlite engine); got it solved by:

  1. downloading, building and installing the latest version of sqlite from here: sqlite-sqlite-amalgamation; before this I had sqlite 3.6.16 on my ubuntu machine; which didn't support foreign keys yet; it should be 3.6.19 or higher to have them working.

  2. installing the latest version of pysqlite from here: pysqlite-2.6.0

after that I started getting exceptions whenever foreign key constraint failed

hope this helps, regards

serge_gubenko
I already have SQLite 3.6.23 and pysqlite 2.6.0 ( and new SQLAlchemy )The SQLite doc says that you must explicitly turn on FK enforcement.In your experience, when it did enforce, did you use that PRAGMA thing?
Nick Perkins
yes, I have "PRAGMA foreign_keys = ON;" in my code
serge_gubenko
+1  A: 

If you need to execute something for setup on every connection, use a PoolListener.

Ants Aasma
Thanks -- I tried the PoolListener, and it did allow me to execute the pragma for every database connection! Perfect! ...except that the pragma still does not work! The SQLite engine still does not enforce foreign keys!...I am still missing a piece of the puzzle. Maybe it's because I am on Windows? The SQLite docs say something about the "build options" that it was built with...but I just got the standard install for Windows...not sure if that matters?
Nick Perkins