Skip to content

Conversation

ismayil-dev
Copy link

@ismayil-dev ismayil-dev commented Jun 11, 2025

Add Database Check Constraints Support

Overview

This PR adds native support for database check constraints to Laravel's Schema Builder, bringing a long-requested feature that enhances data integrity at the database level.

Check constraints allow you to enforce business rules directly in the database, ensuring data consistency regardless of how data is inserted or updated. This is particularly valuable for multi-application environments or when data integrity is critical.

Usage

Schema::create('orders', function (Blueprint $table) {
    $table->id();
    $table->string('status');
    $table->decimal('amount', 10, 2);
    
    $table->checkConstraint('valid_status', function (CheckConstraintBuilder $constraint) {
        $constraint->whereIn('status', ['pending', 'shipped', 'delivered']);
    });
    
    $table->checkConstraint('positive_amount', function (CheckConstraintBuilder $constraint) {
        $constraint->rule('amount', '>', 0);
    });
    
    $table->checkConstraint('shipping_logic', function (CheckConstraintBuilder $constraint) {
        $constraint->where('status', '=', 'shipped', function ($q) {
            $q->rule('shipped_at', 'IS NOT NULL');
        })->orWhere('status', '!=', 'shipped', function ($q) {
            $q->rule('shipped_at', 'IS NULL');
        });
    });
});

// Add to existing tables
Schema::table('users', function (Blueprint $table) {
    $table->checkConstraint('valid_age', function (CheckConstraintBuilder $constraint) {
        $constraint->rule('age', '>=', 18);
    });
});

// Drop constraints
Schema::table('users', function (Blueprint $table) {
    $table->dropConstraint('valid_age');
});

Database Support

Database Create Add/Drop Version
MySQL >= 8.0.16
MariaDB >= 10.2.1
PostgreSQL All
SQL Server All
SQLite ❌* All

*SQLite limitations handled with clear exceptions

Features

  • Fluent API with where(), orWhere(), whereIn(), whereNotIn()
  • Support for all comparison operators and NULL checks
  • Complex nested conditions
  • Full test coverage across all databases
  • Zero breaking changes

Implementation Highlights

  • Consistent API: Follows Laravel's established patterns for schema building
  • Proper Grammar Abstraction: Each database driver handles constraints appropriately
  • Comprehensive Testing: Full test coverage across all supported databases
  • Version Awareness: Uses @RequiresDatabase attributes to ensure compatibility
  • Error Handling: Clear exceptions for unsupported operations (e.g., SQLite limitations)

Testing Note

The current test suite covers the main functionality across all databases. If this feature direction is approved, I can add more comprehensive grammar-specific tests and additional edge case coverage as needed.

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.

@ismayil-dev ismayil-dev marked this pull request as ready for review June 11, 2025 23:28
@ismayil-dev ismayil-dev changed the title feat: Add check constraints support for database schema [12.x] Add check constraints support for database schema Jun 11, 2025
@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 applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@hafezdivandari
Copy link
Contributor

#56929

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