无法删除或更新父行:外键约束失败。虽然存在外键
Cannot delete or update a parent row: a foreign key constraint fails. Foreign keys exist though
错误:
Cannot delete or update a parent row: a foreign key constraint fails
(`databasename`.`webapp_requestsuser`, CONSTRAINT `fk_perRequests` FOREIGN KEY
(`requestsid`) REFERENCES `webapp_requests` (`id`))
创建查询:
CREATE TABLE `webapp_requests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`opentime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`starttime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`description` longtext,
`duration` double DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
CREATE TABLE `webapp_requestsuser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) NOT NULL,
`status` tinyint(4) DEFAULT '0',
`requestsid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `requestsid` (`requestsid`),
CONSTRAINT `fk_perRequests` FOREIGN KEY (`requestsid`) REFERENCES `webapp_requests` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
内容webapp_requests:
_______________________________________
| id = 2 |
| userid = 2 |
| opentime = 2015-11-24 16:26:44 |
| starttime = 2015-11-24 16:26:44 |
| description = This is a description |
| duration = 5.0 |
_______________________________________
内容webapp_requests用户:
_______________________________________
| id = 3 |
| user = 1 |
| status = 0 |
| requestsid = 2 |
_______________________________________
删除查询:
DELETE FROM `webapp_requests` WHERE `id` = 2;
当我想删除 webapp_requests 中的一行时,这就是我一直得到的结果。
我认真地尝试了这里已经提出的每一个可能的问题,但无法解决。是的,我知道 FK 是如何工作的。此外,我使用此功能插入数据:
public function createRequest($userid, $recipients, $description, $duration, $starttime)
{
$existcheck = $this->db->query('SELECT * FROM `webapp_requests` WHERE `userid`=('.$this->db->escape($userid).')');
// Check if already a request is registered
if ($existcheck->num_rows() > 0) {
return false;
}
// First add the request
$query = $this->db->query('INSERT INTO `webapp_requests`( `userid`, `description`, `duration`, `starttime`) VALUES (' . $this->db->escape($userid) . ',' . $this->db->escape($description) . ',' . $this->db->escape($duration) . ',' . $this->db->escape($starttime) . ')');
$id = $this->db->insert_id();
if (!$query) {
return false;
} else {
// Then add the users to the request
$tokens = [];
foreach($recipients as $recipient) {
$this->db->query('INSERT INTO `webapp_requestsuser`(`user`,`requestsid`) VALUES (' . $this->db->escape($recipient) . ',' . $this->db->escape($id) . ')');
$pushValue = $this->db->query('SELECT * FROM `notificationtoken` WHERE `user`=('.$this->db->escape($recipient).')')->row();
array_push($tokens, $pushValue);
}
return $tokens;
}
}
你的 "creates" 工作正常。
问题是您在 referenced table (webapp_requests
) 中删除了违反约束的行。
所以它会在 referencing table (webapp_requestsuser
) 中孤立一行。那里至少会有一行说我的 parent 现在在哪里?我的 requestsid
是 2,但是 referenced table 中没有 id=2 了。
所以 dbengine 说,你告诉我不允许,我不允许。
级联删除
对于那些想要查看级联删除示例的人,请参阅 answer of mine here。
错误:
Cannot delete or update a parent row: a foreign key constraint fails
(`databasename`.`webapp_requestsuser`, CONSTRAINT `fk_perRequests` FOREIGN KEY
(`requestsid`) REFERENCES `webapp_requests` (`id`))
创建查询:
CREATE TABLE `webapp_requests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`opentime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`starttime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`description` longtext,
`duration` double DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
CREATE TABLE `webapp_requestsuser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) NOT NULL,
`status` tinyint(4) DEFAULT '0',
`requestsid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `requestsid` (`requestsid`),
CONSTRAINT `fk_perRequests` FOREIGN KEY (`requestsid`) REFERENCES `webapp_requests` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
内容webapp_requests:
_______________________________________
| id = 2 |
| userid = 2 |
| opentime = 2015-11-24 16:26:44 |
| starttime = 2015-11-24 16:26:44 |
| description = This is a description |
| duration = 5.0 |
_______________________________________
内容webapp_requests用户:
_______________________________________
| id = 3 |
| user = 1 |
| status = 0 |
| requestsid = 2 |
_______________________________________
删除查询:
DELETE FROM `webapp_requests` WHERE `id` = 2;
当我想删除 webapp_requests 中的一行时,这就是我一直得到的结果。
我认真地尝试了这里已经提出的每一个可能的问题,但无法解决。是的,我知道 FK 是如何工作的。此外,我使用此功能插入数据:
public function createRequest($userid, $recipients, $description, $duration, $starttime)
{
$existcheck = $this->db->query('SELECT * FROM `webapp_requests` WHERE `userid`=('.$this->db->escape($userid).')');
// Check if already a request is registered
if ($existcheck->num_rows() > 0) {
return false;
}
// First add the request
$query = $this->db->query('INSERT INTO `webapp_requests`( `userid`, `description`, `duration`, `starttime`) VALUES (' . $this->db->escape($userid) . ',' . $this->db->escape($description) . ',' . $this->db->escape($duration) . ',' . $this->db->escape($starttime) . ')');
$id = $this->db->insert_id();
if (!$query) {
return false;
} else {
// Then add the users to the request
$tokens = [];
foreach($recipients as $recipient) {
$this->db->query('INSERT INTO `webapp_requestsuser`(`user`,`requestsid`) VALUES (' . $this->db->escape($recipient) . ',' . $this->db->escape($id) . ')');
$pushValue = $this->db->query('SELECT * FROM `notificationtoken` WHERE `user`=('.$this->db->escape($recipient).')')->row();
array_push($tokens, $pushValue);
}
return $tokens;
}
}
你的 "creates" 工作正常。
问题是您在 referenced table (webapp_requests
) 中删除了违反约束的行。
所以它会在 referencing table (webapp_requestsuser
) 中孤立一行。那里至少会有一行说我的 parent 现在在哪里?我的 requestsid
是 2,但是 referenced table 中没有 id=2 了。
所以 dbengine 说,你告诉我不允许,我不允许。
级联删除
对于那些想要查看级联删除示例的人,请参阅 answer of mine here。