Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lowercase when looking up self referential foreign key columns #2437

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions enginetest/queries/foreign_key_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,49 @@ var ForeignKeyTests = []ScriptTest{
},
},
},
{
Name: "Self-referential foreign key is not case sensitive",
SetUpScript: []string{
"create table t1 (i int primary key, J int, constraint fk1 foreign key (J) references t1(i));",
"create table t2 (I int primary key, j int, constraint fk2 foreign key (j) references t2(I));",
},
Assertions: []ScriptTestAssertion{
{
// Casing is preserved in show create table statements
Query: "show create table t1;",
Expected: []sql.Row{
{"t1", "CREATE TABLE `t1` (\n `i` int NOT NULL,\n `J` int,\n PRIMARY KEY (`i`),\n KEY `J` (`J`),\n CONSTRAINT `fk1` FOREIGN KEY (`J`) REFERENCES `t1` (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
},
},
{
Query: "insert into t1 values (1, 1);",
Expected: []sql.Row{
{types.NewOkResult(1)},
},
},
{
Query: "insert into t1 values (2, 3);",
ExpectedErr: sql.ErrForeignKeyChildViolation,
},
{
// Casing is preserved in show create table statements
Query: "show create table t2;",
Expected: []sql.Row{
{"t2", "CREATE TABLE `t2` (\n `I` int NOT NULL,\n `j` int,\n PRIMARY KEY (`I`),\n KEY `j` (`j`),\n CONSTRAINT `fk2` FOREIGN KEY (`j`) REFERENCES `t2` (`I`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
},
},
{
Query: "insert into t2 values (1, 1);",
Expected: []sql.Row{
{types.NewOkResult(1)},
},
},
{
Query: "insert into t2d . values (2, 3);",
ExpectedErr: sql.ErrForeignKeyChildViolation,
},
},
},
{
Name: "Cascaded DELETE becomes cascading UPDATE after first child, using ON DELETE for second child",
SetUpScript: []string{
Expand Down
4 changes: 2 additions & 2 deletions sql/plan/foreign_key_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
if reference.ForeignKey.IsSelfReferential() {
allMatch := true
for i := range reference.ForeignKey.Columns {
colPos := reference.SelfCols[reference.ForeignKey.Columns[i]]
refPos := reference.SelfCols[reference.ForeignKey.ParentColumns[i]]
colPos := reference.SelfCols[strings.ToLower(reference.ForeignKey.Columns[i])]
refPos := reference.SelfCols[strings.ToLower(reference.ForeignKey.ParentColumns[i])]
cmp, err := reference.RowMapper.SourceSch[colPos].Type.Compare(row[colPos], row[refPos])
if err != nil {
return err
Expand Down
Loading