Здравствуйте! Есть таблица 'products', в ней внешний ключ 'texture_id' из таблицы 'textures': Код (Text): Schema::table('products', function (Blueprint $table) { $table->unsignedBigInteger('texture_id')->nullable(); $table->foreign('texture_id')->references('id')->on('textures')->onDelete('set null'); }); Необходимо удалить поле 'texture_id' из 'products': Код (Text): Schema::table('products', function (Blueprint $table) { $table->dropForeign(['texture_id']); $table->dropColumn('texture_id'); }); Но появляется ошибка: Код (Text): SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP FOREIGN KEY `products_texture_id_foreign`; check that it exists (SQL: alter table `products` drop foreign key `products_texture_id_foreign`) Что не так то? Я же вижу, что ключ существует. Код (Text): dropForeign('products_texture_id_foreign') также не срабатывает, понятно, что одно и то же, но я уже не знаю, что и делать. От доки толку 0, судя по ней, все должно работать, помогите, пожалуйста.
Зайдите в phpMyAdmin И выполните: Код (Text): SHOW CREATE table `products` Найдите как называется ваш ключ на самом деле и удалите его.
Выполнил, результат - Код (Text): CREATE TABLE `products` ( `id` bigint(20) unsign... дальше не посмотреть. Но в Структуре таблицы и в дереве (слева) он называется именно `products_texture_id_foreign`, разве может как-то по-другому? Даже если в дереве попытаться его редактировать и нажать Предпросмотр SQL - видим Код (Text): ALTER TABLE `tiles_eshop`.`products` DROP INDEX `products_texture_id_foreign`, ADD INDEX `products_texture_id_foreign` (`texture_id`) USING BTREE; . Снова `products_texture_id_foreign`.
Вот полный текст, там такое же имя ключа: Код (Text): CREATE TABLE `products` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `price` decimal(12,2) NOT NULL, `collection_id` bigint(20) unsigned DEFAULT NULL, `color` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `shape_id` bigint(20) unsigned DEFAULT NULL, `material_id` bigint(20) unsigned DEFAULT NULL, `surface_id` bigint(20) unsigned DEFAULT NULL, `texture_id` bigint(20) unsigned DEFAULT NULL, `style_id` bigint(20) unsigned DEFAULT NULL, `length` bigint(20) DEFAULT NULL, `width` bigint(20) DEFAULT NULL, `weight` bigint(20) DEFAULT NULL, `in_box` bigint(20) DEFAULT NULL, `views` bigint(20) DEFAULT 0, `url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `products_code_unique` (`code`), UNIQUE KEY `products_url_unique` (`url`), KEY `products_collection_id_foreign` (`collection_id`), KEY `products_shape_id_foreign` (`shape_id`), KEY `products_material_id_foreign` (`material_id`), KEY `products_surface_id_foreign` (`surface_id`), KEY `products_texture_id_foreign` (`texture_id`), KEY `products_style_id_foreign` (`style_id`), CONSTRAINT `products_collection_id_foreign` FOREIGN KEY (`collection_id`) REFERENCES `collections` (`id`) ON DELETE SET NULL, CONSTRAINT `products_material_id_foreign` FOREIGN KEY (`material_id`) REFERENCES `materials` (`id`) ON DELETE SET NULL, CONSTRAINT `products_shape_id_foreign` FOREIGN KEY (`shape_id`) REFERENCES `shapes` (`id`) ON DELETE SET NULL, CONSTRAINT `products_style_id_foreign` FOREIGN KEY (`style_id`) REFERENCES `styles` (`id`) ON DELETE SET NULL, CONSTRAINT `products_surface_id_foreign` FOREIGN KEY (`surface_id`) REFERENCES `surfaces` (`id`) ON DELETE SET NULL ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci --- Добавлено --- Ничего не понимаю, сейчас выполнил Код (Text): $table->dropColumn('texture_id'); - поле удалилось, вчера тоже так делал, не работало...И куда подевался ключ?
Все, я понял, как надо делать: Сначала отключаем ForeignKeyConstraints, удаляем ключ, и включаем опять: Код (Text): Schema::disableForeignKeyConstraints(); Schema::table('products', function (Blueprint $table) { $table->dropForeign(['texture_id']); }); Schema::enableForeignKeyConstraints(); Schema::table('products', function (Blueprint $table) { $table->dropColumn('texture_id'); }); И все ок.
Это не внешний ключ. Это просто индекс. В приведённом коде с alter table, кстати, об этом прямо написано Вот то, что начинается с "CONSTRAINT" - ограничения по внешнему ключу.