重复的键值违反了唯一约束CakePHP [英] Duplicate key value violates unique constraint, CakePHP

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

问题描述

我已经设置了我的postgres数据库,并应用了所有约束。我在所有桌子上放了蛋糕烤,开始测试。我收到此错误:

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:


错误:SQLSTATE [23505]:唯一违规:7错误:重复键值
违反了唯一约束 fasciculos_pkey1详细信息:键(id)=(7)
已经存在。

Error: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "fasciculos_pkey1" DETAIL: Key (id)=(7) already exists.

SQL查询:


插入 xconv2。 fasciculos(创建, user_id, revista_id,
卷,问题 , 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')

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')

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

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.

有什么帮助吗?

推荐答案

几件事:


  • 我在SQL语句中看不到键7或545;那么这些值从何而来?

  • 创建的列填充有'now()'作为 string ,而不是NOW()函数的值。在PHP代码中用正确的日期时间填充它( date('Ymd H:i:s')通常可以解决问题)。

  • 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).

无论如何,如果您要在循环中插入记录,或者在同一模型中插入多个记录,请不要忘记调用 create() 在模型上,否则ID将相同。

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天全站免登陆