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

Foreign Key Constraint actions don't affect Virtual Column

Details

    Description

      One of my tables has several columns with FK ON DELETE SET NULL constraints on them, plus one virtual column with mildly complex expression based on these FK columns.

      When a DELETE is issued on the foreign table, the FK fields get set to NULL but the STORED Virtual column is not updated.

      There is no trace of this behaviour on the documentation.

      Attachments

        Issue Links

          Activity

            a_marchand_canada Aurelien Marchand added a comment - - edited

            CREATE TABLE `t1` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
             
            CREATE TABLE `t2` (
              `id` int(11) NOT NULL,
              `f_id` int(11) DEFAULT NULL,
              `v_id` int(11) GENERATED ALWAYS AS (`f_id` MOD 2) STORED,
              PRIMARY KEY (`id`),
              KEY `f_id` (`f_id`),
              CONSTRAINT `t2_ibfk_2` FOREIGN KEY (`f_id`) REFERENCES `t1` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
            ) ENGINE=InnoDB;
             
            INSERT INTO t1 VALUES (1),(2),(3),(4);
             
            INSERT INTO t2 VALUES (1,1,NULL), (2,2,NULL),(3,3,NULL),(4,4,NULL);
             
            SELECT * FROM t2;
             
            DELETE FROM t1 WHERE id IN (2,3);
             
            SELECT * FROM t2;

            a_marchand_canada Aurelien Marchand added a comment - - edited CREATE TABLE `t1` ( `id` int (11) NOT NULL , PRIMARY KEY (`id`) ) ENGINE=InnoDB;   CREATE TABLE `t2` ( `id` int (11) NOT NULL , `f_id` int (11) DEFAULT NULL , `v_id` int (11) GENERATED ALWAYS AS (`f_id` MOD 2) STORED, PRIMARY KEY (`id`), KEY `f_id` (`f_id`), CONSTRAINT `t2_ibfk_2` FOREIGN KEY (`f_id`) REFERENCES `t1` (`id`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB;   INSERT INTO t1 VALUES (1),(2),(3),(4);   INSERT INTO t2 VALUES (1,1, NULL ), (2,2, NULL ),(3,3, NULL ),(4,4, NULL );   SELECT * FROM t2;   DELETE FROM t1 WHERE id IN (2,3);   SELECT * FROM t2;

            Thanks for the report, reproducible as described.

            MySQL 5.7 rejects the attempt to create a table with a foreign key (with a cascade action) defined on a base column of a virtual column, even though I can't find this particular limitation in MySQL manual. MySQL 8.0 rejects this CREATE TABLE as well, although it's less verbose about the reason.

            elenst Elena Stepanova added a comment - Thanks for the report, reproducible as described. MySQL 5.7 rejects the attempt to create a table with a foreign key (with a cascade action) defined on a base column of a virtual column, even though I can't find this particular limitation in MySQL manual. MySQL 8.0 rejects this CREATE TABLE as well, although it's less verbose about the reason.
            serg Sergei Golubchik added a comment - - edited

            please, review these five commits:

            b4b547bb14c MDEV-18114 Foreign Key Constraint actions don't affect Virtual Column
            757a0e4cba1 cleanup: key->key_create_info.check_for_duplicate_indexes -> key->old
            e1b389612cd cleanup: Item_field::check_vcol_func_processor()
            a4fd56df7a1 cleanup: put db/table_name into Alter_info
            1633ac631f3 cleanup: reorder enum_fk_option
            

            as usual "cleanup" means "internal code rearrangement, no user visible effects"

            serg Sergei Golubchik added a comment - - edited please, review these five commits: b4b547bb14c MDEV-18114 Foreign Key Constraint actions don't affect Virtual Column 757a0e4cba1 cleanup: key->key_create_info.check_for_duplicate_indexes -> key->old e1b389612cd cleanup: Item_field::check_vcol_func_processor() a4fd56df7a1 cleanup: put db/table_name into Alter_info 1633ac631f3 cleanup: reorder enum_fk_option as usual "cleanup" means "internal code rearrangement, no user visible effects"

            The fix looks correct and exhaustive. Ok to push after the minor issues are resolved

            nikitamalyavin Nikita Malyavin added a comment - The fix looks correct and exhaustive. Ok to push after the minor issues are resolved

            People

              elenst Elena Stepanova
              a_marchand_canada Aurelien Marchand
              Votes:
              0 Vote for this issue
              Watchers:
              5 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.