Re: [Modeling-users] _sqlite.IntegrityError:PRIMARY KEY must be unique
Status: Abandoned
Brought to you by:
sbigaret
From: Mario R. <ma...@ru...> - 2003-08-10 14:49:46
|
On Dimanche, ao=FB 10, 2003, at 16:21, Sebastien Bigaret wrote: > Hi, > > Mario Ruggier <ma...@ru...> wrote: >> I am getting this error (subject line) when I try to insert a new >> object into a table. The PK in question is not a class property, and >> I do not mess with it in any way. The situation is that I had a bunch >> of rows already in this table, and dumped the sql for them to be >> able to recreate them. I then recreated the sqlite db to reflect=20 >> changes >> to the model, and i re-inserted the previously existing rows for the=20= >> one >> table (which is completely untouched by the model changes). Doing >> a "select * from table" gives the rows correctly. >> >> However, since then, the primary key count for newly created objects >> has restarted at 1, and each time i attempt to insert a new object, >> he (the EC) increments this, but since there are already so many in=20= >> the >> db, the insertion fails with the above error. >> >> What's going on? > > The pk generation uses either a sequence or a specific table (sqlite > uses a specific table, because it lacks support for sequences). > > When restoring your db, you need to restore these tables and their > values as well. > > Say you have a table book, then the corresponding table for pk > generation is: PK_SEQ_BOOK > > You can update it by: > > 1. select max(id) from BOOK; > > 2. delete from PK_SEQ_BOOK; > > 3. insert into PK_SEQ_BOOK value(<n>); --where <n> is the result of > step 1. > > Note that if you use inheritance, step 1. should be performed against > all tables in the inheritance tree, and you'll use the maximum of = these > numbers in step 3. Last, the name of the table/sequence if always > PK_SEQ_<name>, where name is in general the external name of the root > entity (see Entity.primaryKeyRootName() and SQLiteAdaptorChannel's > primaryKeysForNewRowsWithEntity()). > > Last note: when using a table for PK generation, it should have only=20= > one > row, this is why step 2. is there. OK, thanks... I was overseeing the seq table. Works perfectly now. mario > -- S=E9bastien. |