A constraint that can say why
Columns and tables take COMMENT; constraints do not, so VIOLATED_CONSTRAINT tells a caller what failed but never what was expected. A request for constraint comments, surfaced in the error.
Source: github.com
Columns and tables take COMMENT; constraints do not. When a CHECK fails, VIOLATED_CONSTRAINT prints the expression and the offending value but not what a valid value should look like. In a system where the schema is the API and callers, increasingly automated, learn what to send from SHOW CREATE TABLE, that is a real gap: one caller sent a UUID where a SHA-256 digest was required and could not tell from the error what to send instead. I asked for COMMENT on a constraint, a comment column in system.constraints, and the comment included in the error text.
A contributor said two days later that they would like to work on it; there is no pull request yet. The issue is open, labelled feature, comp-sql-syntax and external.
On 26.4 the syntax is rejected:
CREATE TABLE cc (x String, CONSTRAINT valid_x CHECK match(x, '^[a-z]+$') COMMENT 'lowercase only')
ENGINE = MergeTree ORDER BY x;
-- Code: 62. DB::Exception: Syntax error ... (COMMENT)
Until then, the only way to get the reason into the error is to put it in the constraint name, e.g. handle_lowercase_2_to_32_chars, which appears in every VIOLATED_CONSTRAINT message.
Discussion
Comments live in GitHub Discussions. Sign in with GitHub to join in.