首页 > 解决方案 > 表中存在键,但在 MySQL 中显示 Can't Drop foreign key

问题描述

“SHOW CREATE TABLE order_products”的结果是

CREATE TABLE `order_products` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `quantity` int(11) NOT NULL,
 `price` double DEFAULT NULL,
 `amount` double(8,2) NOT NULL,
 `product_id` int(10) unsigned NOT NULL,
 `tax_id` int(10) unsigned DEFAULT NULL,
 `discount_id` int(10) unsigned DEFAULT NULL,
 `order_id` int(10) unsigned DEFAULT NULL,
 `username` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `shop_name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `order_products_product_id_foreign` (`product_id`),
 KEY `order_products_tax_id_foreign` (`tax_id`),
 KEY `order_products_discount_id_foreign` (`discount_id`),
 KEY `order_products_order_id_foreign` (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4 
COLLATE=utf8mb4_unicode_ci

我尝试删除我的密钥“order_products_product_id_foreign”

ALTER TABLE order_products DROP FOREIGN KEY order_products_product_id_foreign

但这给了我错误

#1091 - Can't DROP 'order_products_product_id_foreign'; check that column/key exists

标签: phpmysqllaravel

解决方案


您无需在此处指定 FOREIGN KEY 关键字。

alter table order_products drop key order_products_product_id_foreign;

推荐阅读