Skip to content

sql: FK constraints can erroneously use unique indexes on a strict superset of their columns #42680

@thoszhang

Description

@thoszhang

The following works in 19.2.0:

root@127.0.0.1:50371/movr> create table target (a int, b int, unique index (a, b));
CREATE TABLE

Time: 4.44ms

root@127.0.0.1:50371/movr> create table source (a int, index (a));
CREATE TABLE

Time: 3.063ms

root@127.0.0.1:50371/movr> insert into target values (1, 1), (1, 2);
INSERT 2

Time: 3.067ms

root@127.0.0.1:50371/movr> insert into source values (1);
INSERT 1

Time: 3.196ms

root@127.0.0.1:50371/movr> alter table source add foreign key (a) references target (a);
ALTER TABLE

Time: 153.205ms

This is wrong because a unique index on (a, b) doesn't actually enforce uniqueness for (a).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions