Dialect alternatives: upsert is not replace
Overview
Upsert syntax and conflict behavior vary by engine. PostgreSQL and SQLite support ON CONFLICT DO UPDATE; MySQL 8.4 uses ON DUPLICATE KEY UPDATE, which is not an explicit conflict-target selector. PostgreSQL, SQL Server and Oracle provide MERGE with different capabilities. SQLite INSERT OR REPLACE is not the same as an update: a uniqueness conflict can delete the old row and insert a replacement.
First example
BEGIN;INSERT INTO sync_item(sku,price,rev) VALUES ('A',120,2)ON CONFLICT(sku) DO UPDATE SET price=excluded.price,rev=excluded.rev;SELECT id,(SELECT COUNT(*) FROM sync_note) FROM sync_item WHERE sku='A';ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 1, 1 ]]How it works
Both examples write A at price 120. Ordinary DO UPDATE retains ID 1 and its dependent note. REPLACE deletes the conflicting old row; the cascading foreign key deletes the dependent note and the newly inserted row has a different ID in this fixture. Do not use REPLACE as a generic upsert shortcut. INSERT OR IGNORE also has broader error-skipping semantics than a targeted uniqueness DO NOTHING. MySQL may match different unique keys, so avoid ambiguous multiple-key conflicts. Other engines are documentation/conceptual coverage, not runtime verified.
Practical example
BEGIN;INSERT OR REPLACE INTO sync_item(sku,price,rev) VALUES ('A',120,2);SELECT id,(SELECT COUNT(*) FROM sync_note) FROM sync_item WHERE sku='A';ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 3, 0 ]]Practice and mistakes
Practice: compare the dependent note count after both writes. Hint 1: inspect the parent ID. Hint 2: inspect the ON DELETE CASCADE relation. Use explicit dialect choices and test integrity effects rather than mechanically translating SQL keywords.