Bug #91721 | Error adding unique key | ||
---|---|---|---|
Submitted: | 19 Jul 2018 21:09 | Modified: | 30 Oct 2018 13:37 |
Reporter: | Ryan Brothers | Email Updates: | |
Status: | Won't fix | Impact on me: | |
Category: | MySQL Server: DDL | Severity: | S2 (Serious) |
Version: | 5.6, 5.5.60, 5.6.40, 5.7.22,8.0.11 | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[19 Jul 2018 21:09]
Ryan Brothers
[23 Jul 2018 13:06]
MySQL Verification Team
Hello! Thank you for the report and test case. Thanks, Umesh
[30 Oct 2018 13:37]
Karthik Kamath Koteshwar
In a table, internally the primary key is appended to the secondary key. This part of the secondary key is hidden from the user. In the referencing table, there must be a key where the foreign key columns are listed as the first columns in the same order. Such a key is created on the referencing table automatically if it does not exist. This key is internally appended by the primary key but hidden from the user. Till 5.7, InnoDB considers hidden part of this secondary key when determining if it can be a parent key. Since the key and order matches, we ignore the fact that it is a hidden key and allow the foreign key to be created. But the fact that this is a hidden key is taken into consideration when doing Inplace Alter and it rejects the alter. The fix would be to disallow the table creation if composite keys are not specified explicitly, which has been done in 8.0. We thought about backporting but further analysis confirmed that fix would cause change of behavior in earlier GAs which could affect some existing user applications and hence deferred. A simple workaround: . Create parent key explicitly (i.e make hidden part of the key visible) For eg: CREATE TABLE t1 ( t1_id1 INT NOT NULL, PRIMARY KEY(t1_id1) ) ENGINE= InnoDB; CREATE TABLE t2 ( t2_id1 INT, t2_id2 INT, PRIMARY KEY (t2_id1), KEY a(t2_id2, t2_id1), CONSTRAINT FOREIGN KEY (t2_id2) REFERENCES t1 (t1_id1) ) ENGINE= InnoDB; CREATE TABLE t3 ( t3_id1 INT NOT NULL, t3_id2 INT NOT NULL, CONSTRAINT FOREIGN KEY (t3_id1, t3_id2) REFERENCES t2 (t2_id2, t2_id1) ) ENGINE= InnoDB; ALTER TABLE t2 ADD UNIQUE KEY (t2_id2);