php - 键存在于表中,但在 MySQL 中显示 Can't Drop foreign key

标签 php mysql laravel

“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

我尝试删除我的 key “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

最佳答案

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

alter table order_products drop key order_products_product_id_foreign;

关于php - 键存在于表中,但在 MySQL 中显示 Can't Drop foreign key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50655026/

相关文章:

PHP/MYSQL 从一个表获取引用,然后从另一个表获取结果。

php - 选择 - 行换列可以吗?

laravel - Laravel 背包获取最后一个 ID

php - 如何使用 CloudKit Web 服务创建 CKReference?

PHP 过滤文本的禁用词

php - 检查模型 C 是否与任何与模型 A 相关的模型 B 相关

涉及多列的 MySQL 约束

php - 使用查询生成器设置 mysql 用户定义变量

php - 语法错误或访问冲突 : 1055 Expression #17 in group by

php - 如果选择 "image/file"字段的 Laravel 验证?