【问题标题】:"Integrity constraint violation: 1062 Duplicate entry" with Symfony doctrine version able“违反完整性约束:1062 重复条目”与 Symfony 学说版本能够
【发布时间】:2012-06-15 21:24:10
【问题描述】:

通过 Symfony 原则更新表时,我偶尔会收到“违反完整性约束:1062 Duplicate entry ... for key 'PRIMARY'”错误。该表是使用相应的历史表创建的。错误不是来自更新表本身,而是来自在历史表中插入记录。我正在使用 Symfony 1.4,学说 1.2。知道是什么原因造成的吗?谢谢。

$this->computer = $computerTable->findOneByMacAddress($this->props['mac_address']);
$this->computer->ip_address = $this->ip;
$this->computer->setLastCheckinAt(date('Y-m-d H:i:s')) ;
$this->computer->save();

schema.yml
Computer:
    actAs:
    Timestampable: ~
    History:
        className: %CLASS%History
        auditLog: true
        deleteVersions: false
        cascadeDelete: false
columns:
    mac_address:            { type: string(13), notnull: true, }
    last_checkin_at:        { type: string(60), }
    ip_address:             { type: string(40), fixed: false, notnull: false, }
    ...

【问题讨论】:

  • 发布生成错误的代码以及架构的相关部分。另外,您是在尝试手动更新历史记录表,还是在尝试自动更新时抛出异常?
  • $this->computer = $computerTable->findOneByMacaddress($this->props['mac_address']);
  • 嗯?更新您的问题,并且不会进行任何保存,因此它不会产生违反约束的情况...您需要插入、更新或删除才能发生这种情况...
  • 尝试自动更新历史表时抛出异常。
  • 原代码太长,这里就不贴了。我确定返回了正确的记录。该错误是由于将记录保存在历史表中引起的。顺便说一句,这是在负载平衡的环境中。

标签: symfony1 doctrine


【解决方案1】:

很可能findOneByMacAddress 在某些情况下找不到现有记录,并且您有一个要保存到数据库中的空列 mac 地址。

试试这样的:

if(!$this->computer = $computerTable->findOneByMacAddress($this->props['mac_address']))
{
   $this->computer = new Computer();
}
/*do logic here*/
$this->computer->save();

【讨论】:

  • 这不是问题。如果 $this->computer 不为空,则在保存记录之前不会抛出异常。
猜你喜欢
  • 2015-05-18
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多