fix: prevent DROP TABLE when table is referenced by foreign keys

Add foreign key constraint check in translate_drop_table to reject
dropping tables that are referenced by foreign keys when
PRAGMA foreign_keys=ON
This commit is contained in:
joao.faria
2025-11-03 23:41:52 -03:00
parent 2c49c47300
commit 2ba643cd68
3 changed files with 33 additions and 2 deletions

View File

@@ -62,3 +62,24 @@ do_execsql_test_on_specific_db {:memory:} drop-table-after-ops-1 {
DROP TABLE t6;
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='t6';
} {0}
# Test that DROP TABLE fails when table is referenced by foreign keys
do_execsql_test_in_memory_any_error drop-table-fk-constraint-failed {
PRAGMA foreign_keys=ON;
CREATE TABLE parent(a INTEGER PRIMARY KEY);
CREATE TABLE child(a INTEGER PRIMARY KEY, b INTEGER, FOREIGN KEY(b) REFERENCES parent(a));
INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (123, 1);
DROP TABLE parent;
}
# Test that DROP TABLE succeeds when foreign keys are disabled
do_execsql_test_on_specific_db {:memory:} drop-table-fk-disabled-ok {
PRAGMA foreign_keys=OFF;
CREATE TABLE parent(a INTEGER PRIMARY KEY);
CREATE TABLE child(a INTEGER PRIMARY KEY, b INTEGER, FOREIGN KEY(b) REFERENCES parent(a));
INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (123, 1);
DROP TABLE parent;
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='parent';
} {0}