I have a similar situation where I want to remove just the key constrain not the column and not working. I do not know why, maybe you can help me.
Take a look at my code:
--migration for creating the table--
class CreateUnitRoomTypePricesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('unit_room_type_prices', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('unit_room_type_id');
$table->unsignedBigInteger('unit_price_id');
$table->boolean('front')->default(false);
$table->decimal('price_general',10,0)->nullable();
$table->decimal('price_weekdays',10,0)->nullable();
$table->decimal('price_weekend',10,0)->nullable();
$table->timestamps();
});
Schema::table('unit_room_type_prices', function (Blueprint $table) {
$table->foreign('unit_room_type_id')->references('id')->on('unit_room_types');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('unit_room_type_prices');
}
}
--migration for removing the key constrain--
class DropForeignKeyOnUnitRoomTypePrices extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('unit_room_type_prices', function (Blueprint $table) {
$table->dropForeign(['unit_room_type_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
--artisna migrate output--
root@982053685c3f:/var/www/html# php artisan migrate --path=/database/migrations/2022_09_28_141435_drop_foreign_key_on_unit_room_type_prices.php
Migrating: 2022_09_28_141435_drop_foreign_key_on_unit_room_type_prices
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'unit_room_type_prices_unit_room_type_id_foreign'; check that column/key exists (SQL: alter table `unit_room_type_prices` drop foreign key `unit_room_type_prices_unit_room_type_id_foreign`)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
+12 vendor frames
13 database/migrations/2022_09_28_141435_drop_foreign_key_on_unit_room_type_prices.php:18
Illuminate\Support\Facades\Facade::__callStatic()
+21 vendor frames
35 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
root@982053685c3f:/var/www/html#