Skip to content

[10.x] Support check constraints #46512

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

Closed
wants to merge 5 commits into from
Closed

[10.x] Support check constraints #46512

wants to merge 5 commits into from

Conversation

hafezdivandari
Copy link
Contributor

@hafezdivandari hafezdivandari commented Mar 18, 2023

Laravel DB migrations supports almost all column types, indexes and constraints, except CHECK constraint. This PR adds support for check constraint on tables and columns without a breaking change.

Database Supported version on Laravel Check constraint support Drop constraint support
MariaDB 10.3+ ✓ (Docs) ✓ (Docs)
MySQL 5.7+ 8.0.16+ (Docs) 8.0.19+ (Docs)
PostgreSQL 10.0+ ✓ (Docs) ✓ (Docs)
SQLite 3.8.8+ ✓ (Docs) ✗ (Docs)
SQL Server 2017+ ✓ (Docs) ✓ (Docs)

Note: MySQL 5.7 premier support has ended on Oct 2020 and its extended supports ends on Oct 2023.

Examples:

  • Add check constraints when creating table:

    Schema::create('users', function (Blueprint $table) {
        $table->integer('c1')->check('c1 > 10'); // `c1` int not null check (c1 > 10)
        $table->integer('c2');                   // `c2` int not null
        $table->check('c1 + c2 < 100');          // check (c1 + c2 < 100)
        $table->check('c2 <> 0', 'c2_nonzero');  // constraint `c2_nonzero` check (c2 <> 0)
    });
  • Add check constraints when updating table (SQLite doesn't support this):

    Schema::table('users', function (Blueprint $table) {
        $table->check('c1 + c2 < 100');         // add check (c1 + c2 < 100)
        $table->check('c2 <> 0', 'c2_nonzero'); // add constraint `c2_nonzero` check (c2 <> 0)
    });
  • Add a column with check constraint:

    Schema::table('users', function (Blueprint $table) {
        $table->integer('c2')->check('c2 <> 0'); // add `c2` int not null check (c2 <> 0)
    });
  • Modify a column with check constraint (MySQL Only):

    Schema::table('users', function (Blueprint $table) {
        $table->integer('c2')->check('c2 <> 0')->change(); // modify `c2` int not null check (c2 <> 0)
    });
  • Drop a constraint (SQLite doesn't support this):

    Schema::table('users', function (Blueprint $table) {
        $table->dropCheck('c2_nonzero')      // drop constraint `c2_nonzero`
        // Or alternatively:
        $table->dropConstraint('c2_nonzero') // drop constraint `c2_nonzero`
    });

@hafezdivandari hafezdivandari marked this pull request as ready for review March 21, 2023 20:46
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@lorenzolosa
Copy link
Contributor

@hafezdivandari By any chance, have you put this feature on a package or something like that? I'm currently using raw statements to manage check constraints, and it would be nice to have a better integrated solution.

@hafezdivandari
Copy link
Contributor Author

hafezdivandari commented Sep 18, 2024

@lorenzolosa Both Grammar and Blueprint classes are Macroable, so it's easy to extend them and add methods of this PR to each class on your application layer. I didn't see much of a demand from the community on this to release it as a package, maybe reconsidered later.

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