Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johncarmackfan95's avatar

Can't drop foreign keys

I have some data in Page and PageCategory. I am trying to move data around into new tables, so they need to go.

In the original migration:

Schema::table('pages', function (Blueprint $table) {
	$table->bigInteger('page_category_id')->nullable()->unsigned();
});

Schema::table('pages', function (Blueprint $table) {
	$table->foreign('page_category_id')->references('id')->on('page_categories');
});

Trying to remove the 'page_category_id' from my 'pages' table:

Schema::table('pages', function (Blueprint $table) {
	$table->dropForeign(['page_category_id']);
});

Schema::table('pages', function (Blueprint $table) {
	$table->dropColumn('page_category_id');
});

This just gives me an error:

Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'pages_page_category_id_foreign'; check that column/key exists (SQL: alter table `pages` drop foreign key `pages_page_category_id_foreign`)

Not sure what I'm doing wrong. Even when I put the above Schema statements between:

Schema::disableForeignKeyConstraints();
[...]
Schema::enableForeignKeyConstraints();

It still gives the same error.

Laravel 6.x btw.

0 likes
7 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

Did you check that it exists? Maybe it was removed in some of the previous migrations?

johncarmackfan95's avatar

I guess because it failed later into the migration the first time, it did drop the index. So trying to run it again caused the error. Thanks.

ud_supun's avatar

Any solution for this. I'm having the same issue.

anton_khan's avatar

When you use method dropForeign() you have to provide correct column name, for example if you have made foreign key like this:

Schema::table('posts', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');

            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });

Than you have to drop it like this:

Schema::table('posts', function (Blueprint $table) {
                $table->dropForeign('posts_user_id_foreign');
                $table->dropColumn('user_id');
        });
8 likes
molel's avatar

Thank this really came in hand

Please or to participate in this conversation.