Repository navigation
Commit 4d94f39
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
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1820 | 1820 | | |
1821 | 1821 | | |
1822 | 1822 | | |
| 1823 | + | |
1823 | 1824 | | |
1824 | 1825 | | |
1825 | | - | |
| 1826 | + | |
1826 | 1827 | | |
1827 | 1828 | | |
1828 | 1829 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1943 | 1943 | | |
1944 | 1944 | | |
1945 | 1945 | | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
1946 | 1993 | | |
1947 | 1994 | | |
1948 | 1995 | | |
| |||
0 commit comments