mysql - 这个错误是什么意思?
我不需要解决这个错误。我只需要理解它的含义,这样我就可以自己处理它。
Cannot add or update a child row: a foreign key constraint fails
(`db`.`transaction`, CONSTRAINT `transaction_ibfk_2`
FOREIGN KEY (`product_id`) REFERENCES `product` (`id`))
INSERT INTO `transaction` ( `client_id`, `cost`, `website_id`, `product_id`,
`template_number`, `value`, `order_id` )
VALUES ( '17', '', '2480', '', '', '12', '1');
什么是外键?它是如何设置的?
CONSTRAINT transaction_ibfk_2
是什么意思?
这是否意味着我需要一个名为 transaction_ibfk_2
的表?
谢谢。
I do not require this error to be solved. I just need to understand what it means so I can approach it myself.
Cannot add or update a child row: a foreign key constraint fails
(`db`.`transaction`, CONSTRAINT `transaction_ibfk_2`
FOREIGN KEY (`product_id`) REFERENCES `product` (`id`))
INSERT INTO `transaction` ( `client_id`, `cost`, `website_id`, `product_id`,
`template_number`, `value`, `order_id` )
VALUES ( '17', '', '2480', '', '', '12', '1');
What is a foreign key? How is it set?
What does CONSTRAINT transaction_ibfk_2
mean?
Does this mean I need to have a table called transaction_ibfk_2
?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在插入一个空字符串作为
productid
(列表中的第四项)。设置了引用完整性约束以确保您只能插入与引用的
产品
表。You are inserting an empty string as
productid
(Fourth item in the list)There is a referential integrity constraint set up to ensure that you must only insert
productid
s matching one in the referencedproduct
table.这意味着您正在尝试将一个值添加到外键列中,该值在引用列中不可用,或者尝试在外键列中添加空白。
即您试图将product_id 添加为空白,这是不允许的。外键列中的所有值必须是主要引用列 ID 中存在的有效值。
This means you are trying to add a value into the foreign key column which is not available in the referenced column or trying to add blank in foreign key column.
i.e. You are trying to add product_id as blank which is not allowed. All values in foreign key column must be valid values present in the main referenced column id.
约束名称和表之间不一定有关系,尽管大多数人都会适当地命名它们以使他们的生活更轻松。
这意味着您有一个
transaction_ibfk_2
约束。实际的问题在于消息的其余部分:您需要首先在
product
表中插入一行。您插入的id
应该是您尝试插入到transaction
中的product_id
(即''
出于某种原因 - 我非常确定这应该是一个真实值(或可能为 NULL))。您可以使用
create 子句创建外键表
或alter表
DDL 语句。There's not necessarily a relationship between constraint names and tables although most people name them appropriately to make their lives easier.
All this means is that you have a
transaction_ibfk_2
constraint. The actual problem is in the rest of the message:You need to insert a row into your
product
table first. Theid
that you insert there should be theproduct_id
you're trying to insert intotransaction
(which is''
for some reason - I'm pretty certain this should be a real value (or possibly NULL)).You can create foreign keys with a clause of the
create table
oralter table
DDL statements.