Skip to content

[Bug]: CHECK constraints are parsed but not enforced #25099

Open

Description

@dengn

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

v4.0.0-rc3

Commit ID

8.0.30-MatrixOne-v4.0.0-rc3

Other Environment Information

  • Hardware parameters: N/A
  • OS type: N/A
  • Others: Reproduced via cross-language ORM compatibility testing (PHP / Python / Java / Node.js) over the MySQL 8.0.30 wire protocol. The minimal repro below uses raw SQL so it is driver-independent.

Actual Behavior

CHECK is accepted at DDL time but never enforced on writes, so a row that violates it is accepted.

INSERT INTO t VALUES (-5);   -- MatrixOne: accepted

Expected Behavior

The violating row is rejected:

INSERT INTO t VALUES (-5);   -- MySQL 8.0.16+: rejected (ERROR 3819 Check constraint violated)

Steps to Reproduce

CREATE TABLE t (age INT CHECK (age >= 0));
INSERT INTO t VALUES (-5);   -- MySQL 8.0.16+: rejected   MatrixOne: accepted
SELECT * FROM t;             -- MatrixOne: returns the -5 row

Additional information

Application invariants relying on CHECK become silent no-ops.

Background: dengn/php-tester#1.

Activity

  1. changed the title [-][correctness] `CHECK` constraints are parsed but not enforced[/-] [+][Bug]: CHECK constraints are parsed but not enforced[/+] on Jun 23, 2026
  2. added
    kind/bug
    on Jun 23, 2026
  3. added
    severity/s0
    on Jun 30, 2026
  4. added this to the MO-202607 milestone on Jun 30, 2026
  5. added theissue type on Jul 1, 2026
  6. Ariznawlll commented on Jul 2, 2026

    @Ariznawlll
    Collaborator

    Additional Model 11 DDL coverage on latest main 51656deb6 also confirms both named and column-level CHECK constraints are accepted but not enforced, and CREATE TABLE LIKE does not preserve the CHECK metadata.

    drop database if exists mysql_compat_model11b;
    create database mysql_compat_model11b;
    use mysql_compat_model11b;
    
    create table t_base (
      id int primary key,
      c_nn int not null default 5,
      constraint chk_c_nn check (c_nn > 0)
    );
    
    insert into t_base values (1, -1); -- MySQL rejects; MO accepts
    
    create table t_like like t_base;
    show create table t_like;          -- MySQL includes a CHECK constraint; MO does not
    insert into t_like values (2, -1); -- MySQL rejects; MO accepts

    So the issue is not limited to anonymous column-level checks; the DML enforcement and LIKE metadata path are both affected.

  7. removed
    severity/s0
    on Jul 7, 2026
  8. Ariznawlll commented on Aug 6, 2026

    @Ariznawlll
    Collaborator

    在最新 main 60d28f8ecb93ae512da6af4a2915b204a373bd42(2026-08-06,本地双 CN)复验后,当前问题范围可收敛为:CHECK 已在 INSERT 与 INSERT ... ON DUPLICATE KEY UPDATE 中生效,但普通 UPDATE 仍绕过校验并会写入非法数据。

    复现 SQL:

    create table t (
      id int primary key,
      v int not null,
      constraint ck_v check (v between 0 and 10)
    );
    insert into t values (1, 10);
    
    -- 正确:均返回 ERROR 3819,且不写入非法值
    insert into t values (2, 11);
    insert into t values (1, 11)
      on duplicate key update v = values(v);
    
    -- 错误:返回成功,随后读到非法值
    update t set v = 11 where id = 1;
    select id, v from t;
    -- 1 | 11

    在 CN1 → CN2 → CN1 连续 3 轮中结果一致:前两条写入均被 ERROR 3819 ... Check constraint 'ck_v' is violated 拒绝;普通 UPDATE 均返回成功并保存 v = 11。将该非法值提交后重启集群,两个 CN 均读到 1 | 11,说明错误数据已持久化。

    执行计划也能直接区分两条路径:

    • EXPLAIN INSERT 含 Filter Cond: _check_constraint_assert(...);
    • EXPLAIN UPDATE 只有 Multi Update -> Lock -> Project -> Table Scan,没有该断言。

    代码侧对应地,pkg/sql/plan/bind_update.go 在更新行像构造完成后只追加了外键检查(约 434 行),没有调用 appendCheckConstraintPlan;该 helper 当前只被 INSERT/REPLACE 相关路径调用。

    因此建议保留本 issue,并将验收覆盖普通单表 UPDATE、UPDATE ... FROM/多表 UPDATE、带生成列的 UPDATE,以及失败时原行不变。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions