KITFORMA · EDITORIAL GUIDE
Why can INSERT OR REPLACE delete related data in SQLite?
KitForma editorial guide. REPLACE is not simply an in-place update. On certain constraint conflicts it deletes the conflicting row before inserting the replacement, which can interact with foreign keys and triggers.
Step-by-step guidance
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("PRAGMA foreign_keys = ON")
db.executescript("""
CREATE TABLE parent(id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE child(parent_id INTEGER REFERENCES parent(id) ON DELETE CASCADE);
INSERT INTO parent VALUES (1, 'old');
INSERT INTO child VALUES (1);
INSERT OR REPLACE INTO parent VALUES (1, 'new');
""")
assert db.execute("SELECT COUNT(*) FROM child").fetchone()[0] == 0
db.close()
# REPLACE deleted the old parent, triggering the cascade.Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This starter guide was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.