Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 11, 2023

Conversation

phh
Copy link
Contributor

@phh phh commented Jan 10, 2023

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