{"record":{"id":"413cd3b948ec490e","repo":"cakephp/cakephp","slug":"check-constraint-expression-cannot-be-empty","errorCode":null,"errorMessage":"Check constraint expression cannot be empty","messagePattern":"Check constraint expression cannot be empty","errorType":"validation","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Database/Schema/CheckConstraint.php","lineNumber":52,"sourceCode":"     * @param string $expression The check constraint expression (e.g., \"age >= 18\")\n     */\n    public function __construct(\n        protected string $name,\n        protected string $expression,\n    ) {\n    }\n\n    /**\n     * Set the check constraint expression.\n     *\n     * @param string $expression The SQL expression for the check constraint\n     * @return $this\n     * @throws \\InvalidArgumentException\n     */\n    public function setExpression(string $expression)\n    {\n        if (trim($expression) === '') {\n            throw new InvalidArgumentException('Check constraint expression cannot be empty');\n        }\n\n        $this->expression = trim($expression);\n\n        return $this;\n    }\n\n    /**\n     * Get the check constraint expression.\n     *\n     * @return string\n     */\n    public function getExpression(): string\n    {\n        return $this->expression;\n    }\n\n    /**","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Database/Schema/CheckConstraint.php#L34-L70","documentation":"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 ().","triggerScenarios":"Calling $table->addCheck($check) where the CheckConstraint was created via (new CheckConstraint('name', '')) or calling setExpression('') / setExpression('   ') with an empty or whitespace-only string.","commonSituations":"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.","solutions":["Provide a non-empty SQL boolean expression, e.g. setExpression('price > 0')","Check the source of the expression value (config array, env var) for an empty/missing value before constructing the constraint","Skip adding the check constraint entirely when the expression is blank instead of passing ''"],"exampleFix":"// before\n$check = new CheckConstraint('positive_price', '');\n// after\n$check = new CheckConstraint('positive_price', 'price > 0');","handlingStrategy":"validation","validationCode":"if (!isset($expr) || trim((string)$expr) === '') {\n    throw new InvalidArgumentException('Check constraint expression required');\n}\n$check->setExpression($expr);","typeGuard":"function isNonEmptyString($v): bool { return is_string($v) && trim($v) !== ''; }","tryCatchPattern":"try {\n    $check->setExpression($expr);\n} catch (\\InvalidArgumentException $e) {\n    $logger->error('Invalid check constraint: ' . $e->getMessage());\n}","preventionTips":["Validate that expression variables are non-empty strings before constructing constraints","Never pass user/config-supplied strings directly into setExpression without a trim check","Keep expressions as named constants in schema code to avoid empty literals"],"tags":["schema","validation","php"],"backgroundTag":"empty-required-field","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}