vendredi 22 avril 2016

Sqlite Constraint failed on update

The following query somehow gives a Constraint failed error:

UPDATE dimensionitems 
SET    vitemcode = itemcode 
                   || '_' 
                   || Substr('0000' 
                             || (SELECT (SELECT Count(1) 
                                         FROM   dimensionitems t2 
                                         WHERE  t2.itemcode = t1.itemcode 
                                                AND t2.rowid <= t1.rowid) AS no 
                                 FROM   dimensionitems t1 
                                 WHERE  t1.rowid = dimensionitems.rowid 
                                 ORDER  BY t1.itemcode, 
                                           t1.dimvalue1, 
                                           t1.dimvalue2), -4, 4) 

the error: UNIQUE constraint failed: dimensionitems.vitemcode, dimensionitems.dimvalue1, dimensionitems.dimvalue2: UPDATE dimensionitems ...

But when i do a group by over the query result and check duplicates on the column values, i get none:

SELECT Count(*), 
       v2 
FROM   (SELECT itemcode, 
               dimvalue1, 
               dimvalue2, 
               itemcode 
               || '_' 
               || Substr('0000' 
                         || (SELECT (SELECT Count(1) 
                                     FROM   dimensionitems t2 
                                     WHERE  t2.itemcode = t1.itemcode 
                                            AND t2.rowid <= t1.rowid) AS no 
                             FROM   dimensionitems t1 
                             WHERE  t1.rowid = dimensionitems.rowid 
                             ORDER  BY t1.itemcode, 
                                       t1.dimvalue1, 
                                       t1.dimvalue2), -4, 4) AS v2 
        FROM   dimensionitems) r 
GROUP  BY dimvalue1, 
          dimvalue2, 
          v2 
HAVING Count(*) > 1 

gives no result, which means no duplicates right?

0 Rows returned from: select COUNT(*)

Can it be Sqllite update rows one by one and checks integraty one by one?

Notes

  • This constraint is not the primary key.
  • there is also a unique constraint on Vitemcode itself to be always unique

Aucun commentaire:

Enregistrer un commentaire