php - 外键约束失败

标签 php mysql

我正在编写一个有外键错误的新脚本。

我有以下表格:request , categoryrequest_category .

category 现在有 6 个类别,这样就很好了。 我的脚本将数据插入 request表,之后它尝试将条目添加到 request_category但这就是它失败的地方。

代码:

$oUser = new User();
$oUser->firstname = $this->oLibrary->Request->post('firstname');
$oUser->initials  = $this->oLibrary->Request->post('initials');
$oUser->lastname  = $this->oLibrary->Request->post('lastname');
$oUser->zip       = $this->oLibrary->Request->post('zip');
$oUser->city      = $this->oLibrary->Request->post('city');
$oUser->email     = $this->oLibrary->Request->post('email');
$oUser->setStatus(UserStatus::STATUS_PENDING);
$oUser->create();

$oRequest = new ServiceRequest();
$oRequest->title        = $this->oLibrary->Request->post('title');
$oRequest->description  = $this->oLibrary->Request->post('description');
$oRequest->user_id      = $oUser->id;
$oRequest->setStatus(RequestStatus::STATUS_INCOMPLETE);
$oRequest->create();

// above here goes fine

$oRequestCategory = new RequestCategory();
$oRequestCategory->category_id = $this->oLibrary->Request->post('category');
$oRequestCategory->request_id  = $oRequest->id;
$oRequestCategory->create();

我不太明白为什么它不会插入。我已经检查过,两个键都是 request_table要求存在于数据库中。当我尝试在工作台中进行插入时,它会抛出相同的错误,所以我猜这是我的数据库设计中的某个缺陷。有人可以向我解释为什么我会收到此外键错误吗?

错误消息本身:

ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`ruiljedienst`.`request_category`, CONSTRAINT `fk_request_category_category1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

SQL Statement:

INSERT INTO `ruiljedienst`.`request_category` (`category_id`, `request_id`) VALUES ('1', '1')

以下是 MySQL Workbench 中相关数据库表的屏幕截图:

http://prntscr.com/2fwce6

更新:

show create table ruiljedienst.request_category

结果:

CREATE TABLE `request_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) NOT NULL,
  `request_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  KEY `fk_request_category_category1_idx` (`category_id`),
  KEY `fk_request_category_request1_idx` (`request_id`),
  CONSTRAINT `fk_request_category_category1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_request_category_request1` FOREIGN KEY (`request_id`) REFERENCES `request` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1'


select id from ruiljedienst.category

这给出:

http://prntscr.com/2fwhfl

最佳答案

您对 category_id 有外键约束,因此您必须在添加行之前将 id=1 添加到 category 表中到request_category

确保所有引用的表都使用 ENGINE=InnoDB

关于php - 外键约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20889232/

相关文章:

mySQL 查询将团队 ID 更改为团队名称并将 not null 设为空白

php - Joomla:发送邮件需要很长时间

php - 如何使用 MySQLi 向 MySQL 中插入数据?

mysql - 将数据从 Shiny 应用上传到 MySQL 数据库

mysql - 如何从联接中获取仅在一张表中的数据?

mysql - 在MySQL中按子查询的结果排序

PHP 注释格式 : auto generated or User created?

php - 使用 PHP 在 Linux 服务器中压缩 ZIP 文件

php - 这在 php : -> 中叫什么

java - 如何成为管理员?页面