SQLite3 in Python 2.7 Rejecting Foreign Key Insert

llanitedave llanitedave at birdandflower.com
Sun Nov 23 20:29:09 EST 2014


On Sunday, November 23, 2014 12:22:30 AM UTC-8, Frank Millman wrote:
> "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 
> databases.
> 
> Frank Millman

Well in this case the foreign keys ARE on, otherwise it wouldn't be throwing up a foreign key  conflict.  I've got one more thing to try, but so far I still haven't figured out why it's not accepting the foreign key data.  The only thing I can conclude so far is that it chokes if the fields are TEXT.  I've got other foreign keys in the database that are more traditional integer fields, and they seem to be working fine.



More information about the Python-list mailing list