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

Unexpected ER_ERROR_ON_RENAME upon DROP non-existing FOREIGN KEY with ALGORITHM=COPY

Details

    Description

      --source include/have_innodb.inc
       
      CREATE TABLE t1 (a INT) ENGINE=InnoDB;
      --error ER_CANT_DROP_FIELD_OR_KEY
      ALTER TABLE t1 DROP FOREIGN KEY x, ALGORITHM=COPY;
       
      # Cleanup
      DROP TABLE t1;
      

      10.5 8c0b9880

      query 'ALTER TABLE t1 DROP FOREIGN KEY x, ALGORITHM=COPY' failed with wrong errno 1025: 'Error on rename of './test/t1' to './test/#sql2-10fa-4' (errno: 152 "Cannot delete a parent row")', instead of 1091...
      

      Without ALGORITHM=COPY it fails with ER_CANT_DROP_FIELD_OR_KEY as expected.

      There is an ancient upstream bug https://bugs.mysql.com/bug.php?id=14347 which seems related, but I'm not sure it's quite the same.

      Attachments

        Issue Links

          Activity

            While testing my fix I noticed that DROP CONSTRAINT ..., ALGORITHM=COPY has no effect. If you remove ALGORITHM it works as expected.

            CREATE TABLE t1 (a INT primary key, b int) ENGINE=InnoDB;
            CREATE TABLE t2 (a int primary key, b int, constraint b foreign key (b) references t1(a)) engine=innodb;
            SHOW CREATE TABLE t2;
            Table	Create Table
            t2	CREATE TABLE `t2` (
              `a` int(11) NOT NULL,
              `b` int(11) DEFAULT NULL,
              PRIMARY KEY (`a`),
              KEY `b` (`b`),
              CONSTRAINT `b` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1
            ALTER TABLE t2 DROP CONSTRAINT b, ALGORITHM=COPY;
            SHOW CREATE TABLE t2;
            Table	Create Table
            t2	CREATE TABLE `t2` (
              `a` int(11) NOT NULL,
              `b` int(11) DEFAULT NULL,
              PRIMARY KEY (`a`),
              KEY `b` (`b`),
              CONSTRAINT `b` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1
            

            jplindst Jan Lindström (Inactive) added a comment - While testing my fix I noticed that DROP CONSTRAINT ..., ALGORITHM=COPY has no effect. If you remove ALGORITHM it works as expected. CREATE TABLE t1 (a INT primary key, b int) ENGINE=InnoDB; CREATE TABLE t2 (a int primary key, b int, constraint b foreign key (b) references t1(a)) engine=innodb; SHOW CREATE TABLE t2; Table Create Table t2 CREATE TABLE `t2` ( `a` int(11) NOT NULL, `b` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`), CONSTRAINT `b` FOREIGN KEY (`b`) REFERENCES `t1` (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ALTER TABLE t2 DROP CONSTRAINT b, ALGORITHM=COPY; SHOW CREATE TABLE t2; Table Create Table t2 CREATE TABLE `t2` ( `a` int(11) NOT NULL, `b` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`), CONSTRAINT `b` FOREIGN KEY (`b`) REFERENCES `t1` (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
            jplindst Jan Lindström (Inactive) added a comment - http://buildbot.askmonty.org/buildbot/grid?category=main&branch=bb-10.4-MDEV-22230 https://github.com/MariaDB/server/commit/ba80fb383c7172eaa77375d2945fad77829df9c6

            Maybe a more convincing test case would be

            --source include/have_innodb.inc
             
            CREATE TABLE t1 (a INT) ENGINE=InnoDB;
            ALTER TABLE t1 MODIFY COLUMN a VARCHAR(2), DROP FOREIGN KEY IF EXISTS x;
             
            # Cleanup
            DROP TABLE t1;
            

            as it doesn't tamper explicitly with the algorithm and it has "IF EXISTS" for the foreign key drop, so it's a perfectly normal valid ALTER which shouldn't fail. But it does

            11.1 3883eb63

            mysqltest: At line 4: query 'ALTER TABLE t1 MODIFY COLUMN a VARCHAR(2), DROP FOREIGN KEY IF EXISTS x' failed: ER_ERROR_ON_RENAME (1025): Error on rename of './test/t1' to './test/#sql-backup-246583-4' (errno: 152 "Cannot delete a parent row")
            

            elenst Elena Stepanova added a comment - Maybe a more convincing test case would be --source include/have_innodb.inc   CREATE TABLE t1 (a INT ) ENGINE=InnoDB; ALTER TABLE t1 MODIFY COLUMN a VARCHAR (2), DROP FOREIGN KEY IF EXISTS x;   # Cleanup DROP TABLE t1; as it doesn't tamper explicitly with the algorithm and it has "IF EXISTS" for the foreign key drop, so it's a perfectly normal valid ALTER which shouldn't fail. But it does 11.1 3883eb63 mysqltest: At line 4: query 'ALTER TABLE t1 MODIFY COLUMN a VARCHAR(2), DROP FOREIGN KEY IF EXISTS x' failed: ER_ERROR_ON_RENAME (1025): Error on rename of './test/t1' to './test/#sql-backup-246583-4' (errno: 152 "Cannot delete a parent row" )

            The FOREIGN KEY handling was refactored in 10.5.0 by MDEV-20480. I think that it would make sense to fix this only in 10.5 and later major releases, because 10.4 will reach end of life soon.

            marko Marko Mäkelä added a comment - The FOREIGN KEY handling was refactored in 10.5.0 by MDEV-20480 . I think that it would make sense to fix this only in 10.5 and later major releases, because 10.4 will reach end of life soon.

            Needs different patch. So taking over this issue

            thiru Thirunarayanan Balathandayuthapani added a comment - Needs different patch. So taking over this issue

            Patch is in bb-10.4-MDEV-22230

            thiru Thirunarayanan Balathandayuthapani added a comment - Patch is in bb-10.4- MDEV-22230

            This needs some more work, including test coverage to ensure that ALTER TABLE some_table DROP FOREIGN KEY IF EXISTS fk_in_another_table will not drop a constraint by that name from another table.

            marko Marko Mäkelä added a comment - This needs some more work, including test coverage to ensure that ALTER TABLE some_table DROP FOREIGN KEY IF EXISTS fk_in_another_table will not drop a constraint by that name from another table.

            People

              thiru Thirunarayanan Balathandayuthapani
              elenst Elena Stepanova
              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.