Zend_Db_Table 外键约束失败

发布于 2024-12-01 10:43:55 字数 1298 浏览 1 评论 0原文

我必须表,它们的 SQL 如下:

CREATE TABLE item (
itemID INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(100) NOT NULL,
price FLOAT NOT NULL,
stock INTEGER NOT NULL
) ENGINE = InnoDB;


CREATE TABLE salesrecord (
recordID INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
itemID INT UNSIGNED NOT NULL,
recordDate DATE NOT NULL,
FOREIGN KEY (itemID) REFERENCES item (itemID)
) ENGINE = InnoDB;

这是我的 Zend_Db_Table 具体类:

<?php
class Store_Model_Items extends Zend_Db_Table_Abstract
{
  protected $_name = 'item';
  protected $_rowClass = 'Store_Model_Item';
  protected $_dependTables = array('Store_Model_SalesRecords');
}


<?php
class Store_Model_SalesRecords extends Zend_Db_Table_Abstract
{
  protected $_name = 'salesrecord';
  protected $_referenceMap = array(
    'Item' => array(
        'columns' => array('itemID'),
        'refTableClass' => 'Store_Model_Items',
        'refColumns' => array('itemID'),
        'onDelete' => self::CASCADE
    )
  );
} 

当我尝试删除项目表中的一行时, 我收到此消息:

SQLSTATE[HY000]:一般错误:1451 无法删除或更新父行:外键约束失败(newstore/salesrecord,CONSTRAINT salesrecord_ibfk_1 FOREIGN KEY (itemID) 参考 item (itemID))

I have to tables and their SQL is as below:

CREATE TABLE item (
itemID INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(100) NOT NULL,
price FLOAT NOT NULL,
stock INTEGER NOT NULL
) ENGINE = InnoDB;


CREATE TABLE salesrecord (
recordID INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
itemID INT UNSIGNED NOT NULL,
recordDate DATE NOT NULL,
FOREIGN KEY (itemID) REFERENCES item (itemID)
) ENGINE = InnoDB;

And this is my Zend_Db_Table concrete classes:

<?php
class Store_Model_Items extends Zend_Db_Table_Abstract
{
  protected $_name = 'item';
  protected $_rowClass = 'Store_Model_Item';
  protected $_dependTables = array('Store_Model_SalesRecords');
}


<?php
class Store_Model_SalesRecords extends Zend_Db_Table_Abstract
{
  protected $_name = 'salesrecord';
  protected $_referenceMap = array(
    'Item' => array(
        'columns' => array('itemID'),
        'refTableClass' => 'Store_Model_Items',
        'refColumns' => array('itemID'),
        'onDelete' => self::CASCADE
    )
  );
} 

When I try to delete a row in item table,
I get this message:

SQLSTATE[HY000]: General error: 1451 Cannot delete or update a parent row: a foreign key constraint fails (newstore/salesrecord, CONSTRAINT salesrecord_ibfk_1 FOREIGN KEY (itemID) REFERENCES item (itemID))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2024-12-08 10:43:55

如果要删除父表中的记录,首先应删除子表中的相关记录。还可以添加ON DELETE CASCADE检查,这将有助于自动删除子表中的相关记录。尝试以这种方式重新创建您的 FK -

ALTER TABLE salesrecord
  DROP FOREIGN KEY salesrecord_ibfk_1; -- change FK name here!

ALTER TABLE salesrecord
  ADD CONSTRAINT salesrecord_ibfk_1 FOREIGN KEY (itemID)
    REFERENCES item(itemID) ON DELETE CASCADE;

将 'salesrecord_ibfk_1' 更改为实际的 FK 名称。

If you want delete the record from parent table, firstly you should delete related records in a child table. Also you can add ON DELETE CASCADE checking, it will help automatically delete related records in the child table. Try to recreate your FK in this way -

ALTER TABLE salesrecord
  DROP FOREIGN KEY salesrecord_ibfk_1; -- change FK name here!

ALTER TABLE salesrecord
  ADD CONSTRAINT salesrecord_ibfk_1 FOREIGN KEY (itemID)
    REFERENCES item(itemID) ON DELETE CASCADE;

Change 'salesrecord_ibfk_1' with actual FK name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文