Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-11117

CHECK constraint fails on intermediate step of ALTER

Details

    • 10.2.6-3

    Description

      MariaDB [test]> create table t1 (a int, check(a>0 or a is null));
      Query OK, 0 rows affected (0.41 sec)
       
      MariaDB [test]> insert into t1 values (null);
      Query OK, 1 row affected (0.06 sec)
       
      MariaDB [test]> alter table t1 modify a int auto_increment primary key;
      ERROR 4025 (23000): CONSTRAINT `CONSTRAINT_1` failed for `test`.`#sql-4c9e_4`
      

      If there were no CHECK, a column would have been given the value 1 which actually satisfies the condition.

      MariaDB [test]> set check_constraint_checks=0;
      Query OK, 0 rows affected (0.00 sec)
       
      MariaDB [test]> alter table t1 modify a int auto_increment primary key;
      Query OK, 1 row affected (1.01 sec)
      Records: 1  Duplicates: 0  Warnings: 0
       
      MariaDB [test]> select * from t1;
      +---+
      | a |
      +---+
      | 1 |
      +---+
      1 row in set (0.00 sec)
      

      But apparently the column gets the intermediate value 0 which is checked before the auto_increment is applied.

      Attachments

        Issue Links

          Activity

            Hi Sergei,

            I've fixed the bug by disallowing auto-increment columns in CHECK constraints, DEFAULT value expressions and VIRTUAL COLUMN expressions, in commit https://github.com/MariaDB/server/commit/b7aa15458db95149af4f473a90009844a16e9db8. Please review.

            Thanks,
            Jacob

            jacob-mathew Jacob Mathew (Inactive) added a comment - Hi Sergei, I've fixed the bug by disallowing auto-increment columns in CHECK constraints, DEFAULT value expressions and VIRTUAL COLUMN expressions, in commit https://github.com/MariaDB/server/commit/b7aa15458db95149af4f473a90009844a16e9db8 . Please review. Thanks, Jacob

            I have merged the final version of the fix, committed in https://github.com/MariaDB/server/commit/38af34bb2143f2a7ce82d2e241d8995f419a7f29, into the 10.2 branch.

            jacob-mathew Jacob Mathew (Inactive) added a comment - I have merged the final version of the fix, committed in https://github.com/MariaDB/server/commit/38af34bb2143f2a7ce82d2e241d8995f419a7f29 , into the 10.2 branch.

            I have merged the final version of the fix, committed in https://github.com/MariaDB/server/commit/38af34bb2143f2a7ce82d2e241d8995f419a7f29, into the 10.2 branch.

            jacob-mathew Jacob Mathew (Inactive) added a comment - I have merged the final version of the fix, committed in https://github.com/MariaDB/server/commit/38af34bb2143f2a7ce82d2e241d8995f419a7f29 , into the 10.2 branch.
            sashko Oleksandr Diedyk added a comment - - edited

            By making this fix, you've denied using columns in kind of 'recursive' approach.
            I have this table which defines nodes and hierarchy between them

            create table `track_tree` (
                `node_id` bigint not null auto_increment primary key,
                `parent_node_id` bigint,
                constraint `track_tree_node_id_parent_node_id_ck` check (parent_node_id <> node_id)
            )
            

            So check constraint here is not allowed anymore.
            Can you suggest any workaround besides having separate table for "relations" between nodes?

            sashko Oleksandr Diedyk added a comment - - edited By making this fix, you've denied using columns in kind of 'recursive' approach. I have this table which defines nodes and hierarchy between them create table `track_tree` ( `node_id` bigint not null auto_increment primary key, `parent_node_id` bigint, constraint `track_tree_node_id_parent_node_id_ck` check (parent_node_id <> node_id) ) So check constraint here is not allowed anymore. Can you suggest any workaround besides having separate table for "relations" between nodes?

            because of the way auto-increment columns are implemented, neither check constraints, nor virtual columns, not default expressions work with them, unfortunately.

            as a workaround you might try to use sequences, perhaps they'll help.

            serg Sergei Golubchik added a comment - because of the way auto-increment columns are implemented, neither check constraints, nor virtual columns, not default expressions work with them, unfortunately. as a workaround you might try to use sequences, perhaps they'll help.

            People

              jacob-mathew Jacob Mathew (Inactive)
              elenst Elena Stepanova
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.