Skip to content

A foreign key to a primary key with a prefix length compares only the prefix #2883

Closed

Description

@mathiasrw

A foreign key to a primary key with a prefix length, PRIMARY KEY (a(2)), compares only the prefix, so a value that is not in the parent table is accepted.

var db = new alasql.Database();
db.exec('CREATE TABLE p (a STRING, PRIMARY KEY (a(2))); CREATE TABLE c (x STRING REFERENCES p(a))');
db.exec("INSERT INTO p VALUES ('abc')");
db.exec("INSERT INTO c VALUES ('abzzz')");   // accepted

Expected: the second insert is refused, because no row of p has a = 'abzzz'.

Since #2606 (in 4.21.0; before, the prefix was a parse error). The foreign key check seems to look the value up through the key of the parent, which holds only the prefix. A prefix only decides uniqueness in MySQL; a foreign key compares the whole value.

Breaking?

No.

Activity

  1. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #2985

  3. added this to the v4.x Focus milestone on Nov 15, 2026
  4. added a commit that references this issue on Nov 17, 2026
    3b3b8d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions