Repository navigation
Open
0 / 10 of 1 issue completedOpen
0 / 10 of 1 issue completed
Copy link
Milestone
Description
What happens today
Foreign keys are closures in table.checks built by CREATE TABLE in src/statements/createtable.js.
- The column form (
v INT REFERENCES p(u), line 246) replaces the named column with the first primary key column of the parent whenever the parent has one (line 253). This is The column form REFERENCES p(code) checks the primary key of p instead of code #3153. - The table form (
FOREIGN KEY (v) REFERENCES p (u), line 354) keeps the named columns infk.fkcolumns. foreignKeyExists(line 45) looks the values up in the parent's primary key index whenever the parent has a primary key, whatever columns are named (A foreign key to a UNIQUE column rejects values that exist #2661). Only a parent without a primary key is searched row by row.- The parent table is looked up again for every inserted row.
What to change
- At CREATE TABLE, resolve the referenced key once and keep it on the foreign key in
table.schema.foreignKeys(Keep the definition of a table as data in table.schema and build its rules from it #3169). Take the parent's primary key when its columns are the referenced columns or no columns are named, otherwise the parent's UNIQUE key with exactly those columns. Match column names for both forms of UNIQUE (uk.columnsholds names in the column form and objects with anexpressionin the table form). - The check builds the lookup row with the parent's column names in key order, calls the key's
onrightfn, reads that key's index and compares the whole values of the found row, asforeignKeyExistsdoes for prefix keys (A foreign key to a primary key with a prefix length compares only the prefix #2883). Keep a reference to the parent's index and read it throughsyncKeyIndexes(Move the key and check rules of a table into one module #2716). - When the parent has no key on the referenced columns, keep the row-by-row search.
- Keep the error texts (
Foreign key "7" not found in table "p",Invalid foreign key on table ...,FOREIGN KEY allowed only to tables with PRIMARY KEYs).
What stays the same
- A foreign key to the primary key, or to a table without a key, behaves as today.
- A composite foreign key with only some columns NULL still throws
Invalid foreign key on table .... - A foreign key to columns without a key is still accepted at CREATE TABLE.
- Results change for A foreign key to a UNIQUE column rejects values that exist #2661 and The column form REFERENCES p(code) checks the primary key of p instead of code #3153. In the column form a value that exists only in the primary key column is now rejected.
Done when
- A new test file named after this issue covers A foreign key to a UNIQUE column rejects values that exist #2661, The column form REFERENCES p(code) checks the primary key of p instead of code #3153, a composite foreign key to a composite UNIQUE key, a foreign key to the primary key in both forms, a foreign key to a table without a key, the parent data replaced after the foreign key was created, and the error texts.
- The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Fixes
Depends on
Sub-issues
Collapse Sub-issuesSub-issues
Loading 1 sub-issues
Activity
[-]Review #1179 for code improvements: foreign key check bound to the primary key index[/-][+]Check a foreign key against the key of the referenced table that it names[/+]