Skip to content

[9.x] Add withoutForeignKeyConstraints() to disable foreign key constraints in a callback - #45601

Merged
taylorotwell merged 3 commits into
laravel:9.xfrom
phh:patch-1
Jan 11, 2023
Merged

[9.x] Add withoutForeignKeyConstraints() to disable foreign key constraints in a callback#45601
taylorotwell merged 3 commits into
laravel:9.xfrom
phh:patch-1

Conversation

@phh

@phh phh commented Jan 10, 2023

Copy link
Copy Markdown
Contributor

If you want to disable foreign key constraints today you:

Schema::disableForeignKeyConstraints();

Schema::dropIfExists('table1');
Schema::dropIfExists('table2');

Schema::enableForeignKeyConstraints();

My suggestion is to add this in a callback instead of remembering to disable and enable again afterwards:

Schema::withoutForeignKeyConstraints(function () {
    Schema::dropIfExists('table1');
    Schema::dropIfExists('table2');
});

I've not added tests, since I could not find any for disableForeignKeyConstraints() and enableForeignKeyConstraints(). If this idea is something that is interesting I could look into how to add tests for it.

@taylorotwell
taylorotwell merged commit 9983c45 into laravel:9.x Jan 11, 2023
@phh
phh deleted the patch-1 branch January 12, 2023 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants