Details
-
Bug
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.3.6
-
None
Description
Dropping primary key in some cases fails because of foreign constraints on the table of the primary key, which makes no sense.
Test case:
create table test_a (id int primary key); |
create table test_b (id int primary key); |
create table test_a_b (a int not null, |
b int not null, |
constraint ref_a foreign key (a) references test_a (id), |
constraint ref_b foreign key (b) references test_b (id)); |
alter table test_a_b add constraint a_b_pk primary key (a, b); |
alter table test_a_b drop primary key; |
Note that the first four statements succeed, which means that both states without and with primary key on `test_a_b` are consistent. However, the last statement fails. In other words, the database doesn't let me to go from one consistent state to another.
Attachments
Issue Links
- relates to
-
MDEV-16417 Store Foreign Key metadata outside of InnoDB
-
- In Review
-
-
MDEV-17187 table doesn't exist in engine after ALTER other tables with CONSTRAINTs
-
- Closed
-
There is nothing unexpected in this behavior.
When you create `test_a_b` table, indexes on `a` and `b` columns are added automatically to comply with foreign key requirements. If you check the table structure at this point, it looks like this:
(note `ref_a` and `ref_b` keys).
Then, when you add the primary key, `ref_a` key is no longer needed and gets automatically dropped. The table looks like this:
It is expected and documented behavior (the automatic drop is better documented in MySQL manual, while MariaDB KB could use some additions).
But then, you're trying to drop the primary key. It would mean there would be no key on `a` column anymore, and foreign key needs it, hence the problem.
This behavior exists in MariaDB 5.5-10.3 and MySQL 5.6/5.7. MySQL 8.0 (at least 8.0.3 which I have handy), doesn't throw the error, it allows the PK to be dropped, but it also silently drops both foreign keys along with it, so it's hardly a proper fix. However, it hints that some attempt has been made to change the behavior, so I'm assigning it to marko to decide if we want it to be changed on MariaDB. If not, please re-assign it to greenman for extending documentation in regard to automatic key creation/removal.
Update: 8.0.12 is back to the old behavior, it also doesn't allow to drop the PK, so I guess it was just a flaw in an early version.