Skip to content

Commit 4baf7c6

Browse files
authored
lowercase when looking up self referential foreign key columns (#2437)
1 parent 6fccbf3 commit 4baf7c6

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

enginetest/queries/foreign_key_queries.go

+76
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,82 @@ var ForeignKeyTests = []ScriptTest{
16401640
},
16411641
},
16421642
},
1643+
{
1644+
Name: "Self-referential foreign key is not case sensitive",
1645+
SetUpScript: []string{
1646+
"create table t1 (i int primary key, J int, constraint fk1 foreign key (J) references t1(i));",
1647+
"create table t2 (I int primary key, j int, constraint fk2 foreign key (j) references t2(I));",
1648+
"create table t3 (i int primary key, j int, constraint fk3 foreign key (J) references t3(I));",
1649+
},
1650+
Assertions: []ScriptTestAssertion{
1651+
{
1652+
// Casing is preserved in show create table statements
1653+
Query: "show create table t1;",
1654+
Expected: []sql.Row{
1655+
{"t1", "CREATE TABLE `t1` (\n" +
1656+
" `i` int NOT NULL,\n `J` int,\n" +
1657+
" PRIMARY KEY (`i`),\n" +
1658+
" KEY `J` (`J`),\n" +
1659+
" CONSTRAINT `fk1` FOREIGN KEY (`J`) REFERENCES `t1` (`i`)\n" +
1660+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
1661+
},
1662+
},
1663+
{
1664+
Query: "insert into t1 values (1, 1);",
1665+
Expected: []sql.Row{
1666+
{types.NewOkResult(1)},
1667+
},
1668+
},
1669+
{
1670+
Query: "insert into t1 values (2, 3);",
1671+
ExpectedErr: sql.ErrForeignKeyChildViolation,
1672+
},
1673+
{
1674+
// Casing is preserved in show create table statements
1675+
Query: "show create table t2;",
1676+
Expected: []sql.Row{
1677+
{"t2", "CREATE TABLE `t2` (\n" +
1678+
" `I` int NOT NULL,\n" +
1679+
" `j` int,\n" +
1680+
" PRIMARY KEY (`I`),\n" +
1681+
" KEY `j` (`j`),\n" +
1682+
" CONSTRAINT `fk2` FOREIGN KEY (`j`) REFERENCES `t2` (`I`)\n" +
1683+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
1684+
},
1685+
},
1686+
{
1687+
Query: "insert into t2 values (1, 1);",
1688+
Expected: []sql.Row{
1689+
{types.NewOkResult(1)},
1690+
},
1691+
},
1692+
{
1693+
Query: "insert into t2 values (2, 3);",
1694+
ExpectedErr: sql.ErrForeignKeyChildViolation,
1695+
},
1696+
{
1697+
Query: "show create table t3;",
1698+
Expected: []sql.Row{
1699+
{"t3", "CREATE TABLE `t3` (\n" +
1700+
" `i` int NOT NULL,\n `j` int,\n" +
1701+
" PRIMARY KEY (`i`),\n" +
1702+
" KEY `j` (`j`),\n" +
1703+
" CONSTRAINT `fk3` FOREIGN KEY (`j`) REFERENCES `t3` (`i`)\n" +
1704+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
1705+
},
1706+
},
1707+
{
1708+
Query: "insert into t3 values (1, 1);",
1709+
Expected: []sql.Row{
1710+
{types.NewOkResult(1)},
1711+
},
1712+
},
1713+
{
1714+
Query: "insert into t3 values (2, 3);",
1715+
ExpectedErr: sql.ErrForeignKeyChildViolation,
1716+
},
1717+
},
1718+
},
16431719
{
16441720
Name: "Cascaded DELETE becomes cascading UPDATE after first child, using ON DELETE for second child",
16451721
SetUpScript: []string{

sql/plan/foreign_key_editor.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
395395
if reference.ForeignKey.IsSelfReferential() {
396396
allMatch := true
397397
for i := range reference.ForeignKey.Columns {
398-
colPos := reference.SelfCols[reference.ForeignKey.Columns[i]]
399-
refPos := reference.SelfCols[reference.ForeignKey.ParentColumns[i]]
398+
colPos := reference.SelfCols[strings.ToLower(reference.ForeignKey.Columns[i])]
399+
refPos := reference.SelfCols[strings.ToLower(reference.ForeignKey.ParentColumns[i])]
400400
cmp, err := reference.RowMapper.SourceSch[colPos].Type.Compare(row[colPos], row[refPos])
401401
if err != nil {
402402
return err

0 commit comments

Comments
 (0)