Why can REPLACE reset omitted fields to their defaults?
On a uniqueness conflict, REPLACE removes the conflicting row and proceeds with insertion. Omitted insert fields take their defaults.
SQLite example and interpretation
Model A has a checked note. Replacing with only its key restores the default note. Use a targeted update when unrelated fields must survive.
CREATE TABLE m(k TEXT PRIMARY KEY,n TEXT DEFAULT 'new');
INSERT INTO m VALUES('A','checked');
REPLACE INTO m(k) VALUES('A');
SELECT n FROM m;Result [["new"]]
中文
为什么使用 REPLACE 可能让未提供的列恢复默认值?
遇到唯一性冲突时,REPLACE 删除冲突旧行并继续插入新行,未指定的插入字段走默认逻辑。
SQLite 示例与适用条件
模型 A 的人工备注原为 checked,仅重写键后备注变回默认值。需要保留备注时应只更新目标列。
CREATE TABLE m(k TEXT PRIMARY KEY,n TEXT DEFAULT 'new');
INSERT INTO m VALUES('A','checked');
REPLACE INTO m(k) VALUES('A');
SELECT n FROM m;结果 [["new"]]