[MDEV-18320] Foreign key implicit index inconsistency Created: 2019-01-21  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Data Definition - Alter Table
Affects Version/s: 10.3.12
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Remy Fox Assignee: Marko Mäkelä
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-12483 Add foreign keys support for partitio... Stalled

 Description   

MariaDB and MySQL create an 'implicit index', for a foreign key whose columns are not part of an index. That implicit index is removed once the client adds an actual index on the columns involved. However, when the reverse is carried out, i.e. the client removes his actual index again, no implicit index is put back and this causes an error message: Cannot drop index: needed in a foreign key constraint. This is inconvenient for scripts that attempt to reverse actions automatically and there does not seem to be a reason that justifies the inconsistency.

Test script:

	CREATE TABLE a (id INT NOT NULL PRIMARY KEY);
	CREATE TABLE b (id INT NOT NULL PRIMARY KEY);
	ALTER TABLE b ADD COLUMN a_id INT NOT NULL, ADD CONSTRAINT a_id FOREIGN KEY (a_id) REFERENCES a (id);
	ALTER TABLE b ADD INDEX a_id (a_id);

Now try to reverse these actions:

	ALTER TABLE b DROP INDEX a_id;
	ALTER TABLE b DROP COLUMN a_id, DROP FOREIGN KEY a_id;
	DROP TABLE b;
	DROP TABLE a;



 Comments   
Comment by Elena Stepanova [ 2019-01-21 ]

I'll leave it to marko to comment on it.

Comment by Marko Mäkelä [ 2019-02-18 ]

I think that MariaDB should implement foreign keys in the SQL layer, above storage engines. That would also remove the requirement for implicit indexes to exist. Sure, in that case, FOREIGN KEY constraints would be slower to enforce and locking conflicts could become more common, because table or index scans could be needed.

MDEV-12483 already suggests that cross-engine foreign keys should be implemented.

Generated at Thu Feb 08 08:43:12 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.