[MDEV-32018] Allow the setting of Auto_increment on FK referenced columns Created: 2023-08-26  Updated: 2023-10-20  Resolved: 2023-10-20

Status: Closed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.4.31, 10.5.22
Fix Version/s: 10.4.32, 10.5.23, 10.6.16, 10.10.7, 10.11.6, 11.0.4, 11.1.3

Type: Bug Priority: Major
Reporter: Daniel Black Assignee: Daniel Black
Resolution: Fixed Votes: 0
Labels: regression

Issue Links:
Problem/Incident
is caused by MDEV-31086 MODIFY COLUMN can break FK constraint... Closed

 Description   

From horrockss@github in MariaDB docker GH issue 528, it was previously possible in 10.5.21 to add an auto_increment to a FK field under SET FOREIGN_KEY_CHECKS=0.

Since MDEV-31086, this is no longer possible to prevent index corruption. The AUTO_INCREMENT isn't something that can cause corruption however.

Test case:

MariaDB [test]> CREATE TABLE `device_type` (
    ->   `id` int(10) unsigned NOT NULL,
    ->   `name` varchar(128) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
Query OK, 0 rows affected (0.009 sec)
 
MariaDB [test]> 
MariaDB [test]> CREATE TABLE `device_field` (
    ->   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(128) DEFAULT NULL,
    ->   `device_type_id` int(10) unsigned DEFAULT NULL,
    ->   PRIMARY KEY (`id`),
    ->   CONSTRAINT `FK_device_field_device_type` FOREIGN KEY (`device_type_id`) REFERENCES `device_type` (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
Query OK, 0 rows affected (0.000 sec)
 
MariaDB [test]> 
MariaDB [test]> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.000 sec)
 
MariaDB [test]> 
MariaDB [test]> select @@foreign_key_checks;
+----------------------+
| @@foreign_key_checks |
+----------------------+
|                    0 |
+----------------------+
1 row in set (0.000 sec)
 
MariaDB [test]> 
MariaDB [test]> ALTER TABLE device_type MODIFY id INT unsigned AUTO_INCREMENT;
ERROR 1833 (HY000): Cannot change column 'id': used in a foreign key constraint 'FK_device_field_device_type' of table 'test.device_field'

Expected:

MariaDB [test]> CREATE TABLE `device_type` (
    ->   `id` int(10) unsigned NOT NULL,
    ->   `name` varchar(128) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
Query OK, 0 rows affected (0.001 sec)
 
MariaDB [test]> 
MariaDB [test]> CREATE TABLE `device_field` (
    ->   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(128) DEFAULT NULL,
    ->   `device_type_id` int(10) unsigned DEFAULT NULL,
    ->   PRIMARY KEY (`id`),
    ->   CONSTRAINT `FK_device_field_device_type` FOREIGN KEY (`device_type_id`) REFERENCES `device_type` (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
Query OK, 0 rows affected (0.001 sec)
 
MariaDB [test]> 
MariaDB [test]> ALTER TABLE device_type MODIFY id INT unsigned AUTO_INCREMENT;
Query OK, 0 rows affected (0.001 sec)              
Records: 0  Duplicates: 0  Warnings: 0

To preserve the working setup we add this functionality that was previously available before the MDEV-31086 bug fix.



 Comments   
Comment by Daniel Black [ 2023-08-26 ]

Sanja, can you please review https://github.com/MariaDB/server/pull/2739.

Generated at Thu Feb 08 10:28:11 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.