Menu

Deleting a SQLite mirror table nothing had read in a year
📰

Deleting a SQLite mirror table nothing had read in a year

DEV Community: sql·Darkø Tasevski·about 10 hours ago
#EQd6tmyI
#devtosql#devto#dev#table#insert
Long article (1523 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.