Repository navigation
Fix a warning for a foreign key referencing a table with a shorter unique key - #523
Conversation
…ique key When looking for the unique constraint that a foreign key references, each unique index of the referenced table is checked for having the referenced columns as a prefix. An index with fewer columns than the foreign key was read past its end, raising "Undefined array key" warnings. Skip indexes that are shorter than the column list of the reference. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to The change addresses the shorter-index warning while preserving selection of the matching composite key. No actionable merge risk remains from the inspected changes. Pre-merge checks |
|
… case (#534) ## Summary Follow-up to #523. When a foreign key references columns with a different letter case than the referenced table declares them, the referenced unique constraint is not found: ```sql CREATE TABLE t1 (id INT, name VARCHAR(255), PRIMARY KEY (id, name)); CREATE TABLE t2 (id INT, name VARCHAR(255), FOREIGN KEY (id, name) REFERENCES t1 (ID, Name)); SELECT unique_constraint_name FROM information_schema.referential_constraints WHERE table_name = 't2'; -- NULL (MySQL: PRIMARY) ``` MySQL column names are case-insensitive, but the lookup compared the referenced column names to the index columns with `!==`. The foreign key itself works, since SQLite also matches column names case-insensitively; only the information schema entry is wrong. ## Fix The names are compared case-insensitively with `strcasecmp()`, which compares them directly, without creating lowercased copies first. Identifiers are restricted to ASCII in this driver, so this is sufficient. ## Tests `testInformationSchemaForeignKeysReferencingColumnsInDifferentLetterCase` references a single-column primary key, a composite unique key, and a unique key in a foreign key added with `ALTER TABLE`, all with differently cased column names, and checks `UNIQUE_CONSTRAINT_NAME`. It fails on `trunk` with `NULL` for all three. ## 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** * Foreign-key metadata now correctly matches referenced columns when their names use different letter casing. This applies to single-column and composite references, whether constraints are defined during table creation or added afterward. * Information schema results now identify the expected unique indexes for these foreign-key references, including primary-key references. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Jan Jakeš <jan@jakes.pro>
Summary
Creating a multi-column foreign key raises
Undefined array key 1warnings when the referenced table also has a unique key with fewer columns: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_NAMEis still correct, but the warning is emitted, and underWP_DEBUGit 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
testInformationSchemaForeignKeysReferencingShorterUniqueKeychecks that the statement succeeds without a warning and thatREFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAMEis the longer key. It fails ontrunkwith 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
Summary by CodeRabbit