SQLite3 in Python 2.7 Rejecting Foreign Key Insert

Frank Millman frank at chagford.com
Sun Nov 23 09:21:48 CET 2014

"Chris Angelico" <rosuav at gmail.com> wrote in message 
news:CAPTjJmp4Y5zowWn5yFtJutKO4H5jVtqLaNtWqEpA6B35xNdsNA at mail.gmail.com...
> Entirely possible. I never did track down the actual cause of the
> SQLite3 issues my students were having; though I suspect it's not
> purely a Python API issue. I tried to demonstrate the concept of
> foreign keys using the sqlite3 command line tool, and did a sequence
> of commands which ought to have failed, but didn't.

The default is for sqlite3 to ignore foreign key contraints.

To enable them, add the following -

pragma foreign_keys = on;

It works for me.

Unfortunately it has a limitation, which they acknowledge but they say is 
unlikely to be addressed. You can access more than one database concurrently 
by using the 'attach' command, and qualifying a remote tablename as 
{database} dot {tablename}. You can then include the remote table in any sql 
command. However, it will not enforce foreign key constraints across 

Frank Millman

More information about the Python-list mailing list