[MDEV-32270] ALTER TABLE: DROP CONSTRAINT is ignored when ADD CONSTRAINT is also present Created: 2023-09-27 Updated: 2023-11-28 |
|
| Status: | Confirmed |
| Project: | MariaDB Server |
| Component/s: | Data Definition - Alter Table, Storage Engine - InnoDB |
| Affects Version/s: | 10.4, 10.5, 10.6, 10.9, 10.10, 10.11, 10.11.5 |
| Fix Version/s: | 10.4, 10.5, 10.6, 10.11, 11.0 |
| Type: | Bug | Priority: | Major |
| Reporter: | Björn Steinbrink | Assignee: | Vladislav Lesin |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | foreign-keys | ||
| Issue Links: |
|
||||||||
| Description |
|
When an `ALTER TABLE` statement tries to drop an constraint while also adding one, the drop operation is not performed:
|
| Comments |
| Comment by Alice Sherepa [ 2023-09-28 ] | ||||||||||||||||||
|
Thank you! I repeated as described on MariaDB 10.4-11.0 | ||||||||||||||||||
| Comment by Marko Mäkelä [ 2023-09-29 ] | ||||||||||||||||||
|
The native ALTER TABLE in InnoDB seems to work fine, at least in MariaDB Server 10.6, where I tested the following:
If I add , ALGORITHM=COPY to the ALTER TABLE statement, I will get two foreign key constraints. For some reason, on the revision of 10.6 that I tested, if I omit the SET statement, the ALTER TABLE will fail as follows:
The ALGORITHM=COPY version of the operation not only fails to drop the constraint; it will also unnecessarily rebuild the table and copy all data, until MDEV-16356 is fixed. | ||||||||||||||||||
| Comment by Marko Mäkelä [ 2023-09-29 ] | ||||||||||||||||||
|
It actually gets funnier: When I wrote the mtr version of the test, I intended to rename the tables a and b to t1 and t2, respectively. But, I forgot to replace one reference. Because of foreign_key_checks=0, the reference to the missing table a will be allowed. Here is a better test:
The ALGORITHM clauses are redundant, only there to show what is going on. The first ALTER TABLE works correctly; the second one will wrongly end up with two constraints. | ||||||||||||||||||
| Comment by Marko Mäkelä [ 2023-09-29 ] | ||||||||||||||||||
|
If I change the test to use DROP FOREIGN KEY instead of DROP CONSTRAINT, the constraint will be dropped.
The ad-hoc parser in dict_foreign_parse_drop_constraints() looks for DROP followed by FOREIGN KEY. It fails to detect DROP CONSTRAINT or DROP CONSTRAINT IF EXISTS. Also in this case, the SQL layer is definitely validating the constraint names, and InnoDB is duplicating some of that work. I think that dict_foreign_parse_drop_constraints() must be replaced with something that obtains the necessary information from the SQL layer, just like the native ALTER TABLE code path does. |