Skip to content

Commit 4d94f39

Browse files
Nandan108claudeJanJakes
authored
Fix matching a foreign key reference to a unique key with a different 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>
1 parent 81d6afa commit 4d94f39

2 files changed

Lines changed: 49 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 ( 0 !== strcasecmp( $index_columns[ $i ], $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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,53 @@ public function testInformationSchemaForeignKeysReferencingShorterUniqueKey(): v
19431943
);
19441944
}
19451945

1946+
public function testInformationSchemaForeignKeysReferencingColumnsInDifferentLetterCase(): void {
1947+
$this->assertQuery(
1948+
'CREATE TABLE t1 (
1949+
id INT PRIMARY KEY,
1950+
code INT,
1951+
a INT NOT NULL,
1952+
b INT NOT NULL,
1953+
UNIQUE KEY u_code (code),
1954+
UNIQUE KEY u_a_b (a, b)
1955+
)'
1956+
);
1957+
$this->assertQuery(
1958+
'CREATE TABLE t2 (
1959+
t1_id INT,
1960+
x INT,
1961+
y INT,
1962+
FOREIGN KEY (t1_id) REFERENCES t1 (ID),
1963+
FOREIGN KEY (x, y) REFERENCES t1 (A, b)
1964+
)'
1965+
);
1966+
$this->assertQuery( 'ALTER TABLE t2 ADD COLUMN z INT, ADD CONSTRAINT fk_z FOREIGN KEY (z) REFERENCES t1 (CoDe)' );
1967+
1968+
$result = $this->assertQuery(
1969+
"SELECT constraint_name, unique_constraint_name
1970+
FROM information_schema.referential_constraints
1971+
WHERE table_name = 't2'
1972+
ORDER BY constraint_name"
1973+
);
1974+
$this->assertEquals(
1975+
array(
1976+
(object) array(
1977+
'CONSTRAINT_NAME' => 'fk_z',
1978+
'UNIQUE_CONSTRAINT_NAME' => 'u_code',
1979+
),
1980+
(object) array(
1981+
'CONSTRAINT_NAME' => 't2_ibfk_1',
1982+
'UNIQUE_CONSTRAINT_NAME' => 'PRIMARY',
1983+
),
1984+
(object) array(
1985+
'CONSTRAINT_NAME' => 't2_ibfk_2',
1986+
'UNIQUE_CONSTRAINT_NAME' => 'u_a_b',
1987+
),
1988+
),
1989+
$result
1990+
);
1991+
}
1992+
19461993
public function testInformationSchemaAlterTableAddForeignKeys(): void {
19471994
$this->assertQuery( 'CREATE TABLE t1 (id INT PRIMARY KEY, name VARCHAR(255))' );
19481995
$this->assertQuery( 'CREATE TABLE t2 (id INT)' );

0 commit comments

Comments
 (0)