SQLite

View Ticket
Login
Ticket Hash: fc6f41426befdba68558d6ca4da2dd2ba0b5116a
Title: update primary key failure
Status: Closed Type: Incident
Severity: Critical Priority: Immediate
Subsystem: Unknown Resolution: Not_A_Bug
Last Modified: 2009-08-21 08:37:23
Version Found In: 3.6.17
Description:
create table test1(id integer primary key, msg text) update test1 set id=id+1 where id>100

update returned "PRIMARY KEY must be unique"


dan added on 2009-08-21 08:37:23:
This is because SQLite does not defer checking constraints until after the entire statement is executed. For example:

SQLite version 3.6.17
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE t(x INTEGER PRIMARY KEY);
sqlite> INSERT INTO t VALUES(1);
sqlite> INSERT INTO t VALUES(2);
sqlite> UPDATE t SET x=x+1;  
SQL error: PRIMARY KEY must be unique

SQLite modifies the first row of the table, setting its primary key to 2. This clashes with the second row of the table and SQLite throws the "PRIMARY KEY must be unique" exception as a result. Even though if the statement were allowed to complete the final state of the database would not violate the constraint.

This is, unfortunately for those who wish to execute such statements, how SQLite works. It is not considered a bug.