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

[HY000][1553] Changing name of primary key column with foreign key constraint fails.

Details

    Description

      Changing the name of a primary key column with a foreign key constraint fails with the following error since 10.4.13:

      [HY000][1553] (conn=17) Cannot drop index 'PRIMARY': needed in a foreign key constraint

      create schema test collate utf8_unicode_ci;
      create table test.input_type
      (
          input_type_id int(11) unsigned auto_increment
              primary key
      );
       
      create table test.input
      (
          input_id int unsigned auto_increment
              primary key,
          input_type_id int unsigned not null,
          constraint ibfk
              foreign key (input_type_id) references test.input_type (input_type_id)
      );
       
      create index type_id
          on test.input (input_type_id);
       
      alter table test.input_type change input_type_id id int(11) unsigned auto_increment;
      

      Attachments

        Issue Links

          Activity

            mac89 Maarten Manders created issue -
            mac89 Maarten Manders made changes -
            Field Original Value New Value
            Description Changing the name of a primary key column with a foreign key constraint fails with the following error since 10.4.13:

            [HY000][1553] (conn=17) Cannot drop index 'PRIMARY': needed in a foreign key constraint

            {code:mysql}
            create schema test collate utf8_unicode_ci;
            create table test.input_type
            (
                input_type_id int(11) unsigned auto_increment
                    primary key
            );

            create table test.input
            (
                input_id int unsigned auto_increment
                    primary key,
                input_type_id int unsigned not null,
                constraint ibfk
                    foreign key (input_type_id) references test.input_type (input_type_id)
            );

            create index type_id
                on test.input (input_type_id);

            alter table test.input_type change input_type_id id int(11) unsigned auto_increment;
            {code}
            Changing the name of a primary key column with a foreign key constraint fails with the following error since 10.4.13:

            +[HY000][1553] (conn=17) Cannot drop index 'PRIMARY': needed in a foreign key constraint+

            {code:mysql}
            create schema test collate utf8_unicode_ci;
            create table test.input_type
            (
                input_type_id int(11) unsigned auto_increment
                    primary key
            );

            create table test.input
            (
                input_id int unsigned auto_increment
                    primary key,
                input_type_id int unsigned not null,
                constraint ibfk
                    foreign key (input_type_id) references test.input_type (input_type_id)
            );

            create index type_id
                on test.input (input_type_id);

            alter table test.input_type change input_type_id id int(11) unsigned auto_increment;
            {code}
            alice Alice Sherepa made changes -
            Fix Version/s 10.4 [ 22408 ]
            Fix Version/s 10.5 [ 23123 ]
            alice Alice Sherepa made changes -
            Affects Version/s 10.4 [ 22408 ]
            Affects Version/s 10.5 [ 23123 ]
            alice Alice Sherepa made changes -
            Component/s Storage Engine - InnoDB [ 10129 ]
            alice Alice Sherepa made changes -
            Status Open [ 1 ] Confirmed [ 10101 ]
            alice Alice Sherepa made changes -
            Assignee Marko Mäkelä [ marko ]

            Any idea in what version this will be fixed?

            mac89 Maarten Manders added a comment - Any idea in what version this will be fixed?
            anthonyrsl Anthony Rsl added a comment - - edited

            Hello,

            We are also impacted by this issue since we migrated our MariaDB database from 10.4.12 to 10.4.14.

            Here is an example and minimal SQL script to reproduce our issue with a UNIQUE KEY (columns have been anonymised):

            CREATE TABLE `t2` (
              `id` bigint(20) NOT NULL AUTO_INCREMENT,
              PRIMARY KEY (`id`)
            );
             
            CREATE TABLE `t1` (
              `id` bigint(20) NOT NULL AUTO_INCREMENT,
              `parent_id` bigint(20) NOT NULL,
              `number` int(11) NOT NULL,
              PRIMARY KEY (`id`),
              UNIQUE KEY `parent_id` (`parent_id`,`number`),
              CONSTRAINT `fk_t1_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `t2` (`id`) ON DELETE CASCADE
            );
             
            ALTER TABLE t1 CHANGE parent_id parent_id_renamed BIGINT;
            

            With MariaDB 10.5.1 and 10.4.12, the ALTER TABLE t CHANGE statement was correctly executed:

            > ALTER TABLE t1 CHANGE parent_id parent_id_renamed BIGINT;
            Query OK, 0 rows affected (0.046 sec)
            Records: 0  Duplicates: 0  Warnings: 0
            

            And as a result, the parent_id column was renamed to parent_id_renamed.

            Since MariaDB 10.5.2 and 10.4.12, we have now the following error during the ALTER TABLE t CHANGE statement:

            ERROR 1553 (HY000): Cannot drop index 'parent_id': needed in a foreign key constraint
            

            anthonyrsl Anthony Rsl added a comment - - edited Hello, We are also impacted by this issue since we migrated our MariaDB database from 10.4.12 to 10.4.14. Here is an example and minimal SQL script to reproduce our issue with a UNIQUE KEY (columns have been anonymised): CREATE TABLE `t2` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) );   CREATE TABLE `t1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `parent_id` bigint(20) NOT NULL, `number` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `parent_id` (`parent_id`,`number`), CONSTRAINT `fk_t1_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `t2` (`id`) ON DELETE CASCADE );   ALTER TABLE t1 CHANGE parent_id parent_id_renamed BIGINT; With MariaDB 10.5.1 and 10.4.12, the ALTER TABLE t CHANGE statement was correctly executed: > ALTER TABLE t1 CHANGE parent_id parent_id_renamed BIGINT; Query OK, 0 rows affected (0.046 sec) Records: 0 Duplicates: 0 Warnings: 0 And as a result, the parent_id column was renamed to parent_id_renamed. Since MariaDB 10.5.2 and 10.4.12, we have now the following error during the ALTER TABLE t CHANGE statement: ERROR 1553 (HY000): Cannot drop index 'parent_id': needed in a foreign key constraint
            rafal_dobster Rafał Głowacz added a comment - - edited

            As a workaround you can temporarily disable foreign key checks:

            SET FOREIGN_KEY_CHECKS=0;
             
            ALTER TABLE t1 CHANGE parent_id parent_id_renamed BIGINT;
             
            SET FOREIGN_KEY_CHECKS=1;
            

            rafal_dobster RafaÅ‚ GÅ‚owacz added a comment - - edited As a workaround you can temporarily disable foreign key checks: SET FOREIGN_KEY_CHECKS=0;   ALTER TABLE t1 CHANGE parent_id parent_id_renamed BIGINT;   SET FOREIGN_KEY_CHECKS=1;
            alice Alice Sherepa made changes -
            alice Alice Sherepa made changes -
            Labels regression

            There also seems to be no problem using the new

            ALTER TABLE t1 RENAME COLUMN parent_id parent_id_renamed
            

            command though, if you're just renaming it and not changing anything else.

            karly Alexander Karlstad added a comment - There also seems to be no problem using the new ALTER TABLE t1 RENAME COLUMN parent_id parent_id_renamed command though, if you're just renaming it and not changing anything else.
            alice Alice Sherepa added a comment -

            repeatable on 10.4, 10.5 with utf8:

            --source include/have_innodb.inc
             
            create table t1 (id int primary key)
            engine=innodb default charset=utf8;
             
            create table t2 (input_id int primary key, id int not null,
            	key a (id),
                constraint a foreign key (id) references t1  (id)
            )engine=innodb default charset=utf8;
             
            --error 1553
            alter table t1  change id id2 int;
            alter table t1 default charset=latin1;
            alter table t1  change id id2 int;
            

            alter table t1  change id id2 int;
            ERROR HY000: Cannot drop index 'PRIMARY': needed in a foreign key constraint
            alter table t1 default charset=latin1;
            alter table t1  change id id2 int;
            show create table t1;
            Table	Create Table
            t1	CREATE TABLE `t1` (
              `id2` int(11) NOT NULL,
              PRIMARY KEY (`id2`)
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1
            

            alice Alice Sherepa added a comment - repeatable on 10.4, 10.5 with utf8: --source include/have_innodb.inc   create table t1 (id int primary key ) engine=innodb default charset=utf8;   create table t2 (input_id int primary key , id int not null , key a (id), constraint a foreign key (id) references t1 (id) )engine=innodb default charset=utf8; --error 1553 alter table t1 change id id2 int ; alter table t1 default charset=latin1; alter table t1 change id id2 int ; alter table t1 change id id2 int; ERROR HY000: Cannot drop index 'PRIMARY': needed in a foreign key constraint alter table t1 default charset=latin1; alter table t1 change id id2 int; show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `id2` int(11) NOT NULL, PRIMARY KEY (`id2`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1

            Are there, by the way, any known bug reports over at MySQL for the same problems? I tested and had the same issue in MySQL 8, but not in 5.7, so I suspect they have the same issue as well

            karly Alexander Karlstad added a comment - Are there, by the way, any known bug reports over at MySQL for the same problems? I tested and had the same issue in MySQL 8, but not in 5.7, so I suspect they have the same issue as well

            By running git-bisect, I founded that the bug is introduced by df07e00a810890f6f6eb1334c76ee22133750777.

            The following diff is probably the point. old_part.length and new_part.length are equal but old_cs->mbmaxlen and new_cs->mbmaxlen are not in the Alice's test case.

            -  if (old_part.length != new_part.length)
            +  if (old_part.length / old_cs->mbmaxlen != new_part.length / new_cs->mbmaxlen)
            

            (gdb) p old_part.length
            $1 = 4
            (gdb) p new_part.length
            $2 = 4
            (gdb) p old_cs->mbmaxlen
            $3 = 1
            (gdb) p  new_cs->mbmaxlen
            $4 = 3
            

            nayuta-yanagisawa Nayuta Yanagisawa (Inactive) added a comment - By running git-bisect, I founded that the bug is introduced by df07e00a810890f6f6eb1334c76ee22133750777 . The following diff is probably the point. old_part.length and new_part.length are equal but old_cs->mbmaxlen and new_cs->mbmaxlen are not in the Alice's test case. - if (old_part.length != new_part.length) + if (old_part.length / old_cs->mbmaxlen != new_part.length / new_cs->mbmaxlen) (gdb) p old_part.length $1 = 4 (gdb) p new_part.length $2 = 4 (gdb) p old_cs->mbmaxlen $3 = 1 (gdb) p new_cs->mbmaxlen $4 = 3
            serg Sergei Golubchik made changes -
            Priority Major [ 3 ] Critical [ 2 ]

            nayuta-yanagisawa, thank you very much. It looks like the CHARACTER SET is wrongly affecting the check of the INT column.

            bar, can you suggest how we could refine that code to determine the proper length of the column? The intention of the change was to allow an instant conversion from utf8mb3 to utf8mb4. Character sets are not applicable to columns that do not store character data, yet the information appears to be stored for them. How can we detect that old_part and new_part do not store character data, and thus we should ignore any change of character set?

            marko Marko Mäkelä added a comment - nayuta-yanagisawa , thank you very much. It looks like the CHARACTER SET is wrongly affecting the check of the INT column. bar , can you suggest how we could refine that code to determine the proper length of the column? The intention of the change was to allow an instant conversion from utf8mb3 to utf8mb4 . Character sets are not applicable to columns that do not store character data, yet the information appears to be stored for them. How can we detect that old_part and new_part do not store character data, and thus we should ignore any change of character set?
            bar Alexander Barkov made changes -
            Assignee Marko Mäkelä [ marko ] Alexander Barkov [ bar ]
            bar Alexander Barkov made changes -
            Status Confirmed [ 10101 ] In Progress [ 3 ]
            bar Alexander Barkov added a comment - - edited sanja , can you please review? The patch for 10.5 Branch: https://github.com/MariaDB/server/tree/bb-10.5-bar-MDEV-22775 A direct link to the patch: https://github.com/MariaDB/server/commit/ff1ee3fdd92ed5d91fa1641d35c2774fdd3fca13 The patch for 10.4: Branch: https://github.com/MariaDB/server/tree/bb-10.4-bar-MDEV-22775 A direct link to the patch: https://github.com/MariaDB/server/commit/bbd216ec520c0c6ee160602062c6589fddd1d7c6
            bar Alexander Barkov made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            bar Alexander Barkov made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            bar Alexander Barkov made changes -
            Assignee Alexander Barkov [ bar ] Oleksandr Byelkin [ sanja ]
            Status In Progress [ 3 ] In Review [ 10002 ]

            OK to push

            sanja Oleksandr Byelkin added a comment - OK to push
            sanja Oleksandr Byelkin made changes -
            Assignee Oleksandr Byelkin [ sanja ] Alexander Barkov [ bar ]
            Status In Review [ 10002 ] Stalled [ 10000 ]
            bar Alexander Barkov made changes -
            issue.field.resolutiondate 2021-04-07 17:46:15.0 2021-04-07 17:46:15.366
            bar Alexander Barkov made changes -
            Fix Version/s 10.4.19 [ 25205 ]
            Fix Version/s 10.5.10 [ 25204 ]
            Fix Version/s 10.4 [ 22408 ]
            Fix Version/s 10.5 [ 23123 ]
            Resolution Fixed [ 1 ]
            Status Stalled [ 10000 ] Closed [ 6 ]
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 109390 ] MariaDB v4 [ 157892 ]

            People

              bar Alexander Barkov
              mac89 Maarten Manders
              Votes:
              6 Vote for this issue
              Watchers:
              13 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.