问题
I have set my postgres database populated and all constraints applied. I ran cake bake all on all tables and start to test. I got this error:
Error: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "fasciculos_pkey1" DETAIL: Key (id)=(7) already exists.
SQL Query:
INSERT INTO "xconv2"."fasciculos" ("created", "user_id", "revista_id", "volume", "issue", "svol", "snum", "issuedate", "scheme", "reftype", "cliente_id", "output_packtype", "modified") VALUES ('now()', 4, 144, '4', '4', '4', '4', '4', '4', '4', 1, '/var/www', '2013-03-25')
The pk (curr_val = 545) has indeed by default not null and unique constraints but when i tried to add new record using add.ctp, cake doesn't consider the next value of sequence created for the PK. Seems It just start over... I think this is cache problem, but not sure.
Any help?
回答1:
A couple of things:
- I don't see a key 7, or 545 in the SQL statement; so where do these values come from?
- The
createdcolumn gets filled with 'now()' as a string, not a value from the NOW() function. Fill it with the proper datetime in your PHP code (date('Y-m-d H:i:s')usually does the trick).
Anyway, if you are inserting records in a loop, or just more than one with the same model, don't forget to call create() on the model otherwise the ID will be the same.
来源:https://stackoverflow.com/questions/15616617/duplicate-key-value-violates-unique-constraint-cakephp