This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ON DELETE CASCADE does not work when using sqlite3 library
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Vitaminus Maximus, r.david.murray
Priority: normal Keywords:

Created on 2016-01-07 18:57 by Vitaminus Maximus, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (1)
msg257709 - (view) Author: Vitaminus Maximus (Vitaminus Maximus) Date: 2016-01-07 18:57
Let me provide a completely reproducible code, which demonstrates the issue:

    >>> import sqlite3
    >>> cnx = sqlite3.connect("mytest.db")
    >>> cnx.isolation_level = None
    >>> 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, even though I explicitly set isolation_level, start transaction and specify PRAGMA foreign_keys = ON, it does not work. The expected behaviour is that when I run the last SELECT command, it should return only two rows (because a "parent" raw with id = 1 was deleted). By the way, if I run these commands in pure sqlite prompt, then I get what I expect to see.
History
Date User Action Args
2022-04-11 14:58:25adminsetgithub: 70231
2016-01-18 20:22:22r.david.murraysetnosy: + r.david.murray
2016-01-08 07:57:19Vitaminus Maximussetstatus: open -> closed
resolution: not a bug
2016-01-07 18:57:09Vitaminus Maximuscreate