cakephp/cakephp · error · InvalidArgumentException

Check constraint expression cannot be empty

Error message

Check constraint expression cannot be empty

What it means

CheckConstraint::setExpression() validates that the CHECK constraint expression string is non-empty (after trimming). A CHECK constraint must contain a boolean SQL expression, so an empty or whitespace-only string is rejected with InvalidArgumentException. This guards against silently building broken DDL like CHECK ().

Solutions

  1. Provide a non-empty SQL boolean expression, e.g. setExpression('price > 0')
  2. Check the source of the expression value (config array, env var) for an empty/missing value before constructing the constraint
  3. Skip adding the check constraint entirely when the expression is blank instead of passing ''

Example fix

// before
$check = new CheckConstraint('positive_price', '');
// after
$check = new CheckConstraint('positive_price', 'price > 0');
Defensive patterns

Strategy: validation

Validate before calling

if (!isset($expr) || trim((string)$expr) === '') {
    throw new InvalidArgumentException('Check constraint expression required');
}
$check->setExpression($expr);

Type guard

function isNonEmptyString($v): bool { return is_string($v) && trim($v) !== ''; }

Try / catch

try {
    $check->setExpression($expr);
} catch (\InvalidArgumentException $e) {
    $logger->error('Invalid check constraint: ' . $e->getMessage());
}

Prevention

When it happens

Trigger: Calling $table->addCheck($check) where the CheckConstraint was created via (new CheckConstraint('name', '')) or calling setExpression('') / setExpression(' ') with an empty or whitespace-only string.

Common situations: Building schema definitions programmatically where the expression comes from a variable/config that is empty (e.g. missing YAML key, empty env var, null coerced to ''), or copy-pasted constraint scaffolding where the expression was never filled in.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/413cd3b948ec490e. Report an issue: GitHub.

Appendix: source

Thrown at src/Database/Schema/CheckConstraint.php:52

     * @param string $expression The check constraint expression (e.g., "age >= 18")
     */
    public function __construct(
        protected string $name,
        protected string $expression,
    ) {
    }

    /**
     * Set the check constraint expression.
     *
     * @param string $expression The SQL expression for the check constraint
     * @return $this
     * @throws \InvalidArgumentException
     */
    public function setExpression(string $expression)
    {
        if (trim($expression) === '') {
            throw new InvalidArgumentException('Check constraint expression cannot be empty');
        }

        $this->expression = trim($expression);

        return $this;
    }

    /**
     * Get the check constraint expression.
     *
     * @return string
     */
    public function getExpression(): string
    {
        return $this->expression;
    }

    /**

View on GitHub (pinned to 1128eba9b0)