Most of us read INSERT OR REPLACE in SQLite as "upsert" (I did too, until recently), and this post is about what it does instead, and about a table in an iOS app I'm working on that was getting it wrong for a year without anybody noticing, mostly because nobody was looking at the table at all.
TLDR: INSERT OR REPLACE deletes the conflicting row and inserts a new one. With foreign keys on, that delete fires ON DELETE CASCADE on every child table, and an FTS5 table gets a second row instead of an updated one. And before fixing a write path like that, check whether anything reads the table.
Where it came from
I was going through a third-party set of review checklists for Apple-platform code and comparing them with the rules this project already has, which is mostly boring work, since most of the rules overlap. One of them stood out, the one saying that INSERT OR REPLACE is not an upsert.…