Skip to content

Commit 19bc4dc

Browse files
elianddbclaude
andcommitted
Fix enum foreign key constraints to match MySQL behavior
- Allow enum types to reference each other in foreign keys regardless of string values - MySQL allows enum foreign keys to match based on underlying numeric values - Modified foreignKeyComparableTypes to handle enum types specially - Updated test expectations to use correct error types (ErrForeignKeyChildViolation) - Removed Skip flag from 'enums with foreign keys' test 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 53f1886 commit 19bc4dc

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

enginetest/queries/script_queries.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9164,7 +9164,7 @@ where
91649164
},
91659165
},
91669166
{
9167-
Skip: true,
9167+
Skip: false,
91689168
Name: "enums with foreign keys",
91699169
Dialect: "mysql",
91709170
SetUpScript: []string{
@@ -9207,7 +9207,7 @@ where
92079207
},
92089208
{
92099209
Query: "insert into child1 values (3);",
9210-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9210+
ExpectedErr: sql.ErrForeignKeyChildViolation,
92119211
},
92129212
{
92139213
Query: "insert into child1 values ('x'), ('y');",
@@ -9217,7 +9217,7 @@ where
92179217
},
92189218
{
92199219
Query: "insert into child1 values ('z');",
9220-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9220+
ExpectedErr: sql.ErrForeignKeyChildViolation,
92219221
},
92229222
{
92239223
Query: "insert into child1 values ('a');",
@@ -9247,7 +9247,7 @@ where
92479247
},
92489248
{
92499249
Query: "insert into child2 values (3);",
9250-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9250+
ExpectedErr: sql.ErrForeignKeyChildViolation,
92519251
},
92529252
{
92539253
Query: "insert into child2 values ('c');",
@@ -9257,7 +9257,7 @@ where
92579257
},
92589258
{
92599259
Query: "insert into child2 values ('a');",
9260-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9260+
ExpectedErr: sql.ErrForeignKeyChildViolation,
92619261
},
92629262
{
92639263
Query: "select * from child2 order by e;",
@@ -9282,7 +9282,7 @@ where
92829282
},
92839283
{
92849284
Query: "insert into child3 values (3);",
9285-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9285+
ExpectedErr: sql.ErrForeignKeyChildViolation,
92869286
},
92879287
{
92889288
Query: "insert into child3 values ('x'), ('y');",
@@ -9292,11 +9292,11 @@ where
92929292
},
92939293
{
92949294
Query: "insert into child3 values ('z');",
9295-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9295+
ExpectedErr: sql.ErrForeignKeyChildViolation,
92969296
},
92979297
{
92989298
Query: "insert into child3 values ('a');",
9299-
ExpectedErr: sql.ErrForeignKeyParentViolation,
9299+
ExpectedErr: sql.ErrForeignKeyChildViolation,
93009300
},
93019301
{
93029302
Query: "select * from child3 order by e;",

sql/plan/alter_foreign_key.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ func foreignKeyComparableTypes(ctx *sql.Context, type1 sql.Type, type2 sql.Type)
655655
if type1String.Collation().CharacterSet() != type2String.Collation().CharacterSet() {
656656
return false
657657
}
658+
case sqltypes.Enum:
659+
// Enum types can reference each other in foreign keys regardless of their string values.
660+
// MySQL allows enum foreign keys to match based on underlying numeric values.
661+
return true
658662
default:
659663
return false
660664
}

0 commit comments

Comments
 (0)