Here I provide a completely reproducible code, which demonstrates the whole issue. And the issue is that, when I delete rows from the "parent" table, corresponding rows from the "child" table are not deleted, even though they have a foreign key with ON DELETE CASCADE
specified. So this is the code:
>>> import sqlite3
>>> cnx = sqlite3.connect("mytest.db")
>>> cursor = cnx.cursor()
>>> cnx.execute("BEGIN")
<sqlite3.Cursor object at 0x7f0ab0923490>
>>> cursor.execute("CREATE TABLE test_table (id integer)")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> cursor.execute("CREATE UNIQUE INDEX id_primary ON test_table(id)")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> cursor.execute("INSERT INTO test_table (id) VALUES (1),(2),(3)")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> cursor.execute("CREATE TABLE test_table_2(id_fk integer, txt text, FOREIGN KEY (id_fk) REFERENCES test_table(id) ON DELETE CASCADE)")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> cursor.execute("INSERT INTO test_table_2 (id_fk, txt) VALUES (1,\"one\"),(2,\"two\"),(3,\"three\")")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> res = cursor.execute("SELECT * FROM test_table_2")
>>> res
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> for r in res:
... print(r)
...
(1, 'one')
(2, 'two')
(3, 'three')
>>> cursor.execute("PRAGMA foreign_keys = ON")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> cursor.execute("DELETE FROM test_table WHERE id = 1")
<sqlite3.Cursor object at 0x7f0ab0923420>
>>> res = cursor.execute("SELECT * FROM test_table_2")
>>> for r in res:
... print(r)
...
(1, 'one')
(2, 'two')
(3, 'three')
As you can see, I even explicitly run PRAGMA foreign_keys = ON
before I do DELETE
, but it does not help. BTW, if I run these commands in sqlite3>
prompt, then everything is ok. So, the whole problem is with Python
library.
Aucun commentaire:
Enregistrer un commentaire