Skip to content

Commit 5deb4d6

Browse files
Nandan108claude
andauthored
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/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,11 @@ private function extract_referential_constraint_data( WP_Parser_Node $node, stri
18061806
// Find which index includes referenced column names as a prefix.
18071807
$unique_constraint_name = null;
18081808
foreach ( $index_columns_map as $index_name => $index_columns ) {
1809+
// An index with fewer columns than the reference can't have them as a prefix.
1810+
if ( count( $index_columns ) < count( $reference_parts ) ) {
1811+
continue;
1812+
}
1813+
18091814
$is_prefix = true;
18101815
foreach ( $reference_parts as $i => $reference_part ) {
18111816
if ( $index_columns[ $i ] !== $this->get_value( $reference_part ) ) {

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,35 @@ public function testInformationSchemaForeignKeysWithMultipleColumns(): void {
19141914
);
19151915
}
19161916

1917+
public function testInformationSchemaForeignKeysReferencingShorterUniqueKey(): void {
1918+
// The referenced table also has a unique key with fewer columns than the reference.
1919+
$this->assertQuery(
1920+
'CREATE TABLE t1 (
1921+
id INT,
1922+
name VARCHAR(255),
1923+
UNIQUE KEY u_id (id),
1924+
UNIQUE KEY u_id_name (id, name)
1925+
)'
1926+
);
1927+
$this->assertQuery(
1928+
'CREATE TABLE t2 (
1929+
id INT,
1930+
name VARCHAR(255),
1931+
FOREIGN KEY (id, name) REFERENCES t1 (id, name)
1932+
)'
1933+
);
1934+
1935+
$result = $this->assertQuery( "SELECT unique_constraint_name FROM information_schema.referential_constraints WHERE table_name = 't2'" );
1936+
$this->assertEquals(
1937+
array(
1938+
(object) array(
1939+
'UNIQUE_CONSTRAINT_NAME' => 'u_id_name',
1940+
),
1941+
),
1942+
$result
1943+
);
1944+
}
1945+
19171946
public function testInformationSchemaAlterTableAddForeignKeys(): void {
19181947
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY, name VARCHAR(255))' );
19191948
$this->assertQuery( 'CREATE TABLE t2 (id INT)' );

0 commit comments

Comments
 (0)