Note that there are some explanatory texts on larger screens.

plurals
  1. POsqlite3 foreign keys "remembered"?
    primarykey
    data
    text
    <p>I really, really hope I'm simply doing something wrong.</p> <p>I have two tables and a mapping table; the latter's two columns reference the respective primary keys of the others. Deleting one of the data rows does not work if a mapping exists; this is expected. However, deleting the mapping should allow me to delete the data row, but I get an IntegrityError when I try to do so.</p> <p>Example code:</p> <pre><code>import sqlite3 conn = sqlite3.connect(':memory:') conn.execute('PRAGMA foreign_keys = ON') fk = (conn.execute("PRAGMA foreign_keys").fetchone()[0]) print 'version = %s, foreign keys = %r' % (sqlite3.sqlite_version, bool(fk)) if not fk: raise Exception('No foreign keys!?') c = conn.cursor() c.executescript(''' create table if not exists main.one (resource_id TEXT PRIMARY KEY, data TEXT); create table if not exists main.two (node_id INTEGER PRIMARY KEY, data TEXT); create table if not exists main.mapping (node_id INTEGER REFERENCES two, resource_id TEXT REFERENCES one); insert into main.one(resource_id, data) values('A', 'A one thing'); insert into main.two(node_id, data) values(1, 'A two thing'); insert into main.mapping(resource_id, node_id) values('A', 1); insert into main.one(resource_id, data) values('B', 'Another one thing'); insert into main.two(node_id, data) values(2, 'Another two thing'); insert into main.mapping(resource_id, node_id) values('B', 2); insert into main.one(resource_id, data) values('C', 'Yet another one thing'); ''') for tbl in 'one', 'two', 'mapping': print 'TABLE main.%s:\n%s\n' % (tbl, '\n'.join(repr(r) for r in c.execute('select * from main.%s' % tbl).fetchall())) del_cmd = """delete from main.one where resource_id='B'""" print 'Attempting: %s' % (del_cmd,) try: c.execute(del_cmd) except Exception, e: print 'Failed to delete: %s' % e cmd = """delete from main.one where resource_id='C'""" print 'Attempting: %s' % (cmd,) c.execute(cmd) cmd = """delete from main.mapping where resource_id='B' AND node_id=2""" print '\nAttempting: %s' % (cmd,) c.execute(cmd) for tbl in 'one', 'two', 'mapping': print 'TABLE main.%s:\n%s\n' % (tbl, '\n'.join(repr(r) for r in c.execute('select * from main.%s' % tbl).fetchall())) print 'Attempting: %s' % (del_cmd,) c.execute(del_cmd) </code></pre> <p>I'd expect all of that to print the various table contents and progress. However, the very last DELETE violates a foreign key constraint that I cannot find:</p> <pre><code>$ python test.py version = 3.6.22, foreign keys = True TABLE main.one: (u'A', u'A one thing') (u'B', u'Another one thing') (u'C', u'Yet another one thing') TABLE main.two: (1, u'A two thing') (2, u'Another two thing') TABLE main.mapping: (1, u'A') (2, u'B') Attempting: delete from main.one where resource_id='B' Failed to delete: foreign key constraint failed Attempting: delete from main.one where resource_id='C' Attempting: delete from main.mapping where resource_id='B' AND node_id=2 TABLE main.one: (u'A', u'A one thing') (u'B', u'Another one thing') TABLE main.two: (1, u'A two thing') (2, u'Another two thing') TABLE main.mapping: (1, u'A') Attempting: delete from main.one where resource_id='B' Traceback (most recent call last): File "/tmp/test.py", line 49, in &lt;module&gt; c.execute(del_cmd) sqlite3.IntegrityError: foreign key constraint failed </code></pre> <p>WTF?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload