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?
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.
Next add a PoolListener:
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. Addingnullable=Falseto someForeignKey()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:
Here
wall_idandtype_idare bothForeignKey()‘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.