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

[11.x] Fix adding constraints on sqlite #49728

Conversation

hafezdivandari
Copy link
Contributor

@hafezdivandari hafezdivandari commented Jan 17, 2024

SQLite schema grammar silently ignores adding primary key and foreign key constraints when altering table, this PR fixes that. I understand that this was to preventing failure when running tests using SQLite, but please consider that it's more important to let the user know that these constraints are not actually added. A simple workaround for users that use SQLite for running tests is to simply check for their database before adding constraints:

Schema::table('posts', function (Blueprint $table) {
    // ...

    if (! Schema::getConnection() instanceof \Illuminate\Database\SQLiteConnection) {
        $table->foreign('user_id')->references('id')->on('users');
    }
});

Or alternatively if #49723 merged:

Schema::table('posts', function (Blueprint $table) {
    // ...

    if (! DB::is('sqlite')) {
        $table->foreign('user_id')->references('id')->on('users');
    }
});

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@hafezdivandari hafezdivandari marked this pull request as ready for review January 17, 2024 21:45
@taylorotwell
Copy link
Member

I don't see a big reason to change this atm.

@hafezdivandari hafezdivandari deleted the master-fix-add-constraints-sqlite branch January 18, 2024 14:38
@hafezdivandari
Copy link
Contributor Author

hafezdivandari commented Jan 18, 2024

@taylorotwell this was to fix a long time issue #24876

@hafezdivandari hafezdivandari restored the master-fix-add-constraints-sqlite branch January 18, 2024 17:00
@hafezdivandari hafezdivandari deleted the master-fix-add-constraints-sqlite branch January 19, 2024 09:18
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.

3 participants