【问题标题】:Foreign key constraint failure [duplicate]外键约束失败[重复]
【发布时间】:2014-01-20 06:31:31
【问题描述】:

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

我有以下表格:request、category 和 request_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 需要的两个键都存在于数据库中。当我尝试在 Workbench 中进行插入时,它会引发相同的错误,所以我想这是我的数据库设计中的某个缺陷。有人可以向我解释为什么我会收到这个外键错误吗?

错误信息本身:

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工作台中相关数据库表的截图:

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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您在category_id 上有一个外键约束,因此您必须先将id=1 添加到category 表中,然后再将行添加到request_category。

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

    【讨论】:

    • 感谢您的快速响应,但类别表中有 6 个条目,ID 为 1 到 6 prntscr.com/2fwg5d
    • 错误信息说没有。你能用show create table request_category和select id from category更新你的问题吗?
    • 尝试在工作台的 request_category 中创建您的第一条记录,有时您必须在自动增量列中添加第一个值,然后才能自动生成其余值
    • 给了我同样的错误:(
    • 好吧,你找到我了,我想不通。
    猜你喜欢
    • 2019-04-03
    • 2013-05-29
    • 1970-01-01
    • 2013-03-04
    • 2016-08-17
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多