无法设置关系 phpmyadmin

cannot set relation phpmyadmin

我有一个问题。我尝试在我的数据库中创建一个关系:

我想将供应商列中的 ID 设置为我的订单列中 suppliers_id 的外键。

但我收到黄色字段中显示的错误。 外键设置为空,但即使我禁用该选项,它仍然会出错。

希望有人能帮忙

这是我的 table 定义:

===Database inventory_test

== Table structure for table mutaties

|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|product_id|int(11)|No|
|aantal|int(11)|No|
|date|timestamp|No|CURRENT_TIMESTAMP
|omschrijving|varchar(255)|No|
== Table structure for table orders

|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|ordernummer|varchar(255)|No|
|product_id|int(11)|No|
|aantal|int(11)|No|
|omschrijving|varchar(255)|No|
|supplier_id|int(11)|Yes|NULL
|geleverd|tinyint(1)|No|0
|date|timestamp|No|CURRENT_TIMESTAMP
== Table structure for table products

|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|merk|varchar(190)|No|
|model|varchar(190)|No|
|threshold|int(11)|No|
== Table structure for table suppliers

|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|naam|int(11)|No|

问题是订单列包含 suppliers_id 列中值为 0 的数据。但是 0 值不存在于供应商 table 的 id 列中,因此当您尝试创建外键时会出错。

您需要将订单table中的suppliers_id列设置为空,然后添加外键。要将 suppliers_id 设置为空 运行 此查询:update orders set supplier_id=null。另请参阅 MySQL Foreign Key

上的本教程