Repository navigation
Commit 5deb4d6
Fix a warning for a foreign key referencing a table with a shorter unique key (#523)
## Summary
Creating a multi-column foreign key raises `Undefined array key 1`
warnings when the referenced table also has a unique key with fewer
columns:
```sql
CREATE TABLE p (a INT NOT NULL, b INT NOT NULL, UNIQUE KEY ua (a), UNIQUE KEY uab (a, b));
CREATE TABLE c (a INT NOT NULL, b INT NOT NULL, FOREIGN KEY (a, b) REFERENCES p (a, b));
-- Warning: Undefined array key 1 in class-wp-sqlite-information-schema-builder.php
```
When looking for the unique constraint a foreign key references, each
unique index of the referenced table is checked for having the
referenced columns as a prefix. A shorter index is read past its end.
The resulting `UNIQUE_CONSTRAINT_NAME` is still correct, but the warning
is emitted, and under `WP_DEBUG` it is printed into the page.
## Fix
Indexes with fewer columns than the reference are skipped, since they
cannot have the referenced columns as a prefix.
## Tests
`testInformationSchemaForeignKeysReferencingShorterUniqueKey` checks
that the statement succeeds without a warning and that
`REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME` is the longer key. It
fails on `trunk` with the warning (PHPUnit converts warnings to
exceptions).
`packages/mysql-on-sqlite`: 1044 tests pass (PHP 8.3, SQLite 3.45.1);
PHPCS is clean.
Found while running WooCommerce on WordPress Playground.
## Use of AI
This change was prepared with Claude Code (Claude Fable 5.1). I reviewed
the code, the tests, and this description, and I take responsibility for
the change.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Corrected foreign-key metadata to report the unique constraint that
matches the referenced columns when multiple unique constraints exist.
This prevents a shorter, non-matching constraint from being selected and
improves the accuracy of constraint information exposed by the database.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>1 parent ed86f8b commit 5deb4d6
2 files changed
Lines changed: 34 additions & 0 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1806 | 1806 | | |
1807 | 1807 | | |
1808 | 1808 | | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
1809 | 1814 | | |
1810 | 1815 | | |
1811 | 1816 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1914 | 1914 | | |
1915 | 1915 | | |
1916 | 1916 | | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
1917 | 1946 | | |
1918 | 1947 | | |
1919 | 1948 | | |
| |||
0 commit comments