phalcon/cphalcon · error · InvalidCheckExpression
CHECK expression must be a non-empty string
Error message
CHECK expression must be a non-empty string
What it means
Even when definition['expression'] exists, Phalcon\Db\Check rejects values that are not strings or that are the empty string. A CHECK constraint needs a non-empty SQL boolean expression, so integers, nulls, arrays, or '' all throw InvalidCheckExpression at construction time.
Source
Thrown at phalcon/Db/Check.zep:76
* prefix in that case.
*
* @var string
*/
protected name;
/**
* Phalcon\Db\Check constructor
*/
public function __construct( string name, array definition)
{
var expression;
if unlikely !fetch expression, definition["expression"] {
throw new CheckExpressionRequired();
}
if unlikely typeof expression != "string" || expression === "" {
throw new InvalidCheckExpression();
}
let this->name = name;
let this->expression = expression;
}
/**
* Returns the CHECK expression
*/
public function getExpression() -> string
{
return this->expression;
}
/**
* Returns the constraint name (may be an empty string for unnamed)
*/
public function getName() -> stringView on GitHub (pinned to b7419de9cd)
Solutions
- Normalize the expression to a non-empty string: $expr = trim((string) $expr); and reject '' explicitly.
- Validate external input before building the Check: is_string($expr) && $expr !== ''.
- Reject constraint config with empty expressions at load time instead of at DDL time.
Example fix
// before
$expr = $config['check'] ?? '';
$check = new Check('chk_price', ['expression' => $expr]); // '' throws InvalidCheckExpression
// after
$expr = trim((string) ($config['check'] ?? ''));
if ($expr === '') {
throw new InvalidArgumentException('Check expression missing in config');
}
$check = new Check('chk_price', ['expression' => $expr]); Defensive patterns
Strategy: type-guard
Validate before calling
if (!is_string($expr) || $expr === '') {
throw new InvalidArgumentException('CHECK expression must be a non-empty string');
}
$check = new Check('chk', ['expression' => $expr]); Type guard
function isNonEmptyString(mixed $value): bool
{
return is_string($value) && $value !== '';
} Prevention
- Normalize external config values to strings, rejecting empty ones at the boundary.
- Trim and assert non-empty before constructing constraint objects.
- Add schema validation for constraint config files.
When it happens
Trigger: new Check('chk', ['expression' => 0]) or ['expression' => null] after a config miss; expressions built by concatenation that yield ''; JSON payloads where a numeric value stays numeric; using the empty string as a 'no constraint' marker.
Common situations: Config-driven constraints where a missing value falls back to '' via ?? ''; API/JSON inputs keeping scalars typed; template concatenation producing empty output; misunderstanding null vs '' semantics (null key absent is a different exception).
Related errors
- CHECK expression is required
- Column generation expression must be a string
- Dropping a CHECK constraint is not supported by SQLite
- DATABASE PARAMETER 'FOREIGN_KEY_CHECKS' HAS TO BE 1
- The table must contain at least one column
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/dd3b19c01d73fb30.
Report an issue: GitHub.