cakephp重复键值违反唯一约束 [英] cakephp duplicate key value violates unique constraint

查看:572
本文介绍了cakephp重复键值违反唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定...我已设置我的postgres数据库填充和所有约束应用。 OS ...我跑蛋糕烘烤所有的表,开始测试。我收到此错误:



错误:SQLSTATE [23505]:唯一违规:7错误:重复的键值违反唯一约束fasciculos_pkey1DETAIL:Key



SQL查询:INSERT INTOxconv2。fasciculos(created,user_id,revista_id,volume发布,svol,snum,issate,scheme,reftype,cliente_id,output_packtype,modified)VALUES('now()',4,144, ,'4','4','4','4','4','4',1,'/ var / www','2013-03-25')



pk(curr_val = 545)默认不是null和唯一约束,但是当我试图使用add.ctp添加新记录时,蛋糕不考虑为PK创建的序列的下一个值。似乎它刚刚开始...我认为这是缓存问题,但不确定。



some1有帮助吗?



ty

解决方案




  • 我在SQL语句中看不到键7或545;那么这些值来自哪里?

  • 创建的列以'now()'作为 / strong>,而不是来自NOW()函数的值。在你的PHP代码中填充正确的datetime( date('Ymd H:i:s')通常会做到这一点)。



无论如何,如果你在一个循环中插入记录,或者只是多个具有相同模型的记录,不要忘了调用 create() ,否则ID将是相同的。


Ok ... i have set my postgres database populated and all constraints applied. OS ... 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.

Can some1 help ?

ty

解决方案

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 created column 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.

这篇关于cakephp重复键值违反唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆