Skip to content

Commit 84b1693

Browse files
Nandan108claude
authored andcommitted
Fix matching a foreign key reference to a unique key with a different case
When looking for the unique constraint that a foreign key references, the referenced column names were compared to the index columns case-sensitively. MySQL column names are case-insensitive, so a reference like "REFERENCES t1 (ID)" to a column "id" found no unique constraint, and UNIQUE_CONSTRAINT_NAME in information_schema.referential_constraints was NULL. Compare the column names case-insensitively. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 81d6afa commit 84b1693

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,9 +1820,10 @@ private function extract_referential_constraint_data( WP_Parser_Node $node, stri
18201820
continue;
18211821
}
18221822

1823+
// Column names are case-insensitive in MySQL.
18231824
$is_prefix = true;
18241825
foreach ( $reference_parts as $i => $reference_part ) {
1825-
if ( $index_columns[ $i ] !== $this->get_value( $reference_part ) ) {
1826+
if ( strtolower( $index_columns[ $i ] ) !== strtolower( $this->get_value( $reference_part ) ) ) {
18261827
$is_prefix = false;
18271828
break;
18281829
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,27 @@ public function testInformationSchemaForeignKeysReferencingShorterUniqueKey(): v
19431943
);
19441944
}
19451945

1946+
public function testInformationSchemaForeignKeysReferencingDifferentlyCasedColumns(): void {
1947+
$this->assertQuery( 'CREATE TABLE t1 (id INT, name VARCHAR(255), PRIMARY KEY (id, name))' );
1948+
$this->assertQuery(
1949+
'CREATE TABLE t2 (
1950+
id INT,
1951+
name VARCHAR(255),
1952+
FOREIGN KEY (id, name) REFERENCES t1 (ID, Name)
1953+
)'
1954+
);
1955+
1956+
$result = $this->assertQuery( "SELECT unique_constraint_name FROM information_schema.referential_constraints WHERE table_name = 't2'" );
1957+
$this->assertEquals(
1958+
array(
1959+
(object) array(
1960+
'UNIQUE_CONSTRAINT_NAME' => 'PRIMARY',
1961+
),
1962+
),
1963+
$result
1964+
);
1965+
}
1966+
19461967
public function testInformationSchemaAlterTableAddForeignKeys(): void {
19471968
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY, name VARCHAR(255))' );
19481969
$this->assertQuery( 'CREATE TABLE t2 (id INT)' );

0 commit comments

Comments
 (0)