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

When updating cascaded foreign keys, closing foreign key constraints is ignored and invalid.

Details

    Description

      table1:

         CREATE TABLE users (
        id INT PRIMARY KEY,
        name VARCHAR(50)
      );
      

      table2

          CREATE TABLE orders (
        id INT PRIMARY KEY,
        user_id INT,
        order_date DATE,
        FOREIGN KEY (user_id) REFERENCES users(id)
      );
      

      Update:

       ALTER TABLE users  MODIFY COLUMN  `id` tinyint(11) unsigned NOT NULL auto_increment;
       ERROR 1833 (HY000): Cannot change column 'id': used in a foreign key constraint 'orders_ibfk_1' of table 'runzi.orders'
       
       
      MariaDB [runzi]> SET FOREIGN_KEY_CHECKS=0;
      Query OK, 0 rows affected (0.001 sec)
       
      MariaDB [runzi]> ALTER TABLE users  MODIFY COLUMN  `id` tinyint(11) unsigned NOT NULL auto_increment;
      ERROR 1833 (HY000): Cannot change column 'id': used in a foreign key constraint 'orders_ibfk_1' of table 'runzi.orders'
      MariaDB [runzi]>
      

      Attachments

        Issue Links

          Activity

            Runzi Runzi added a comment -

            I am not sure whether this is expected or a bug. maybe my approach is incorrect?

            Runzi Runzi added a comment - I am not sure whether this is expected or a bug. maybe my approach is incorrect?

            MDEV-31086 changed this. Let me partially quote my comment there:

            It turns out in the SQL layer, the function fk_check_column_changes() would intentionally cause ALTER TABLE…ALGORITHM=COPY to ignore invalid foreign key constraints if foreign_key_checks=OFF. This would seem to be misguided, because a subsequent TRUNCATE TABLE or OPTIMIZE TABLE (if alter_algorithm=copy) and possibly ALTER TABLE…FORCE, ALGORITHM=COPY would be refused on the table.

            When foreign_key_checks=OFF, I think that it is reasonable to allow CREATE TABLE or ALTER TABLE to create FOREIGN KEY constraints that refer to non-existing tables. But I do not see the usefulness of allowing the creation of totally invalid foreign key constraints, that is, when columns are of incompatible type or use incompatible character encodings or collations.

            I think a good guideline is that the output of SHOW CREATE TABLE after any number of DDL operations will be a valid CREATE TABLE statement if foreign_key_checks=OFF.

            If someone wants to change the collation or encoding of a column that is connected to a FOREIGN KEY constraint, that can be done with 3 ALTER TABLE statements.

            In MDEV-31987 you can find an example of using 3 ALTER TABLE statements:

            1. drop the foreign key constraint and modify the column in the child table
            2. modify the column in the (former) parent table
            3. add the constraint back to the child table

            The benefit of using foreign_key_checks=off while doing this is that that the constraint can be added instantly, without any validation. Currently (until MDEV-16356 is fixed), the validation involves an unnecessary table rebuild.

            marko Marko Mäkelä added a comment - MDEV-31086 changed this. Let me partially quote my comment there: It turns out in the SQL layer, the function fk_check_column_changes() would intentionally cause ALTER TABLE…ALGORITHM=COPY to ignore invalid foreign key constraints if foreign_key_checks=OFF . This would seem to be misguided, because a subsequent TRUNCATE TABLE or OPTIMIZE TABLE (if alter_algorithm=copy ) and possibly ALTER TABLE…FORCE, ALGORITHM=COPY would be refused on the table. When foreign_key_checks=OFF , I think that it is reasonable to allow CREATE TABLE or ALTER TABLE to create FOREIGN KEY constraints that refer to non-existing tables. But I do not see the usefulness of allowing the creation of totally invalid foreign key constraints, that is, when columns are of incompatible type or use incompatible character encodings or collations. I think a good guideline is that the output of SHOW CREATE TABLE after any number of DDL operations will be a valid CREATE TABLE statement if foreign_key_checks=OFF . If someone wants to change the collation or encoding of a column that is connected to a FOREIGN KEY constraint, that can be done with 3 ALTER TABLE statements. In MDEV-31987 you can find an example of using 3 ALTER TABLE statements: drop the foreign key constraint and modify the column in the child table modify the column in the (former) parent table add the constraint back to the child table The benefit of using foreign_key_checks=off while doing this is that that the constraint can be added instantly, without any validation. Currently (until MDEV-16356 is fixed), the validation involves an unnecessary table rebuild.
            Runzi Runzi added a comment -

            thanks, therefore, when foreign_key_checks=OFF is used, only the check of add/del foreign key constraint is ignored, instead of the check of modification or update. If I need to update, I can only implement it by deleting/adding logic, right?
            Is this a long-term solution?

            Runzi Runzi added a comment - thanks, therefore, when foreign_key_checks=OFF is used, only the check of add/del foreign key constraint is ignored, instead of the check of modification or update. If I need to update, I can only implement it by deleting/adding logic, right? Is this a long-term solution?

            I do not foresee any changes to this logic in the near term. Implementing transactional DDL (so that the 2 ALTER TABLE could be executed as one atomic unit) would be a huge undertaking and could be years ahead.

            marko Marko Mäkelä added a comment - I do not foresee any changes to this logic in the near term. Implementing transactional DDL (so that the 2 ALTER TABLE could be executed as one atomic unit) would be a huge undertaking and could be years ahead.
            Runzi Runzi added a comment -

            I see, thank you for your reply

            Runzi Runzi added a comment - I see, thank you for your reply

            People

              Unassigned Unassigned
              Runzi Runzi
              Votes:
              0 Vote for this issue
              Watchers:
              3 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.