mysql - 这个错误是什么意思?

发布于 2024-09-19 20:00:30 字数 573 浏览 9 评论 0原文

我不需要解决这个错误。我只需要理解它的含义,这样我就可以自己处理它。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界如花海般美丽 2024-09-26 20:00:30

您正在插入一个空字符串作为 productid(列表中的第四项)。

INSERT INTO transaction  
(client_id, cost, website_id, product_id, template_number, value, order_id) 
VALUES ('17', '', '2480', '', '', '12', '1')

设置了引用完整性约束以确保您只能插入与引用的产品表。

You are inserting an empty string as productid (Fourth item in the list)

INSERT INTO transaction  
(client_id, cost, website_id, product_id, template_number, value, order_id) 
VALUES ('17', '', '2480', '', '', '12', '1')

There is a referential integrity constraint set up to ensure that you must only insert productids matching one in the referenced product table.

十秒萌定你 2024-09-26 20:00:30

这意味着您正在尝试将一个值添加到外键列中,该值在引用列中不可用,或者尝试在外键列中添加空白。
即您试图将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.

人间不值得 2024-09-26 20:00:30

约束名称和表之间不一定有关系,尽管大多数人都会适当地命名它们以使他们的生活更轻松。

这意味着您有一个 transaction_ibfk_2 约束。实际的问题在于消息的其余部分:

FOREIGN KEY (product_id) REFERENCES product  (id)

您需要首先在 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:

FOREIGN KEY (product_id) REFERENCES product  (id)

You need to insert a row into your product table first. The id that you insert there should be the product_id you're trying to insert into transaction (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 or alter table DDL statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文