Laravel drop foreign Key in Migration

laravel create migration and model
laravel migration
laravel 6 migration
cannot drop index needed in a foreign key constraint laravel
laravel migration drop column
laravel migration change column type
laravel migration relationships
laravel migration rename table

I want to create a Migration which shall drop a table. I created the Migration like this:

Schema::table('devices', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('client_id')->nullable();
    $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
});

And now I try to drop it like this:

    Schema::table('devices', function (Blueprint $table) {
        $table->dropForeign('devices_client_id_foreign');
        $table->drop('devices');
    });

But I get following error:

In Connection.php line 664:

  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'devices_client_id_foreign'; check that column/key exists (SQL:

alter table devices drop foreign key devices_client_id_foreign)

In PDOStatement.php line 144:

  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'devices_client_id_foreign'; check that column/key exists


In PDOStatement.php line 142:

  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'devices_client_id_foreign'; check that column/key exists

You just need to disable foreign key checks before you drop the table then enable them again after like this:

DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::dropIfExists('devices');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');

A "hidden" way to drop foreign keys in migrations, When I try to reverse this migration by running php artisan migrate:rollback command, I get a Rolled back message, but it's not actually doing  2) Go to the Laravel migration script (or create one). The trick is to first drop the foreign key relationship and then drop the column . public function down()


You can use this answer. https://stackoverflow.com/a/30177480/8513937

Pass to dropForeign the column name as array. Internally, Laravel drops the associated foreign key.

$table->dropForeign(['client_id']);

Dropping column with foreign key Laravel error: General error: 1025 , 2) Go to the Laravel migration script (or create one). The trick is to first drop the foreign key relationship and then drop the column. Laravel drop foreign Key in Migration. Ask Question Asked 1 year, 5 months ago. Active 1 year, 5 months ago. Viewed 4k times 6. I want to create a Migration which


Just drop the whole table (documentation):

Schema::drop('devices');

Laravel Migrations: how to drop a foreign key column, Sometimes it can be agreeable to remove a database column that hosts a foreign key relationship, and we add wrong column with foreign key  Today I was trying out Laravel's Migration. I tried to create two tables with the second table having a foreign key that reference the first table.


Try this ways...

  public function down()
 {
    Schema::dropIfExists('devices');
 }

//Or this
  public function down(){
    Schema::table('devices', function (Blueprint $table) {
        $table->dropForeign(['client_id']);
        $table->dropColumn('client_id');
        $table->drop('devices');
    });
  }

Laravel, Laravel Version: 5.4.35 PHP Version: 7.0.13 Database Driver & Version: in a migration, a key seems to stay in the table, like so: KEY  I need to change this table and drop the foreign key reference & column pick_detail_id and add a new varchar column called sku after pick_id column. So, I've created another migration, which looks like this:


Dropping a foreign constraint with $table->dropForeign() leaves a , How do I drop Foreign Key constraints with Migrations? Laravel Docs says (https:​//laravel.com/docs/6.x/migrations#foreign-key-constraints): To  To drop a table in laravel, Create a first migration $ php artisan make:migration drop_user_table Laravel cannot add foreign key constraint. 3.


How do I drop Foreign Key constraints with Migrations?, I am trying to drop the foreign key and add the foreign key in one migration but can't get it to work: I have created the table contents with the fore As of Laravel 5.8, migration stubs use the bigIncrements method on ID columns by default. Previously, ID columns were created using the increments method. This will not affect any existing code in your project; however, be aware that foreign key columns must be of the same type.


Laravel, Laravel drop foreign key constraint – We can use migration to drop foreign key constraint using the artisan command. Here in this tutorial we  And the main reason is that Laravel created messages table and then tried to create foreign key constraint to table message_types that don’t exists yet. If you look at your any migration file name, you can see that it starts from the date and time. For example create_users_table : 2014_10_12_000000_create_users_table