Zend_Db_Table 外键约束失败
我必须表,它们的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要删除父表中的记录,首先应删除子表中的相关记录。还可以添加ON DELETE CASCADE检查,这将有助于自动删除子表中的相关记录。尝试以这种方式重新创建您的 FK -
将 '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 -
Change 'salesrecord_ibfk_1' with actual FK name.