Skip to content

Commit ea0916e

Browse files
elianddbclaude
andcommitted
Fix DECIMAL foreign key error message format to match MySQL
- Update ErrForeignKeyChildViolation error message to match MySQL format exactly - Include database name, table name, constraint name, and column names - Error now shows: "cannot add or update a child row: a foreign key constraint fails (`db`.`table`, CONSTRAINT `name` FOREIGN KEY (`cols`) REFERENCES `parent` (`parent_cols`))" - Maintains functional behavior while providing MySQL-compatible error messages 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d8ad856 commit ea0916e

File tree

4 files changed

+15
-67
lines changed

4 files changed

+15
-67
lines changed

enginetest/queries/foreign_key_queries.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,66 +2956,4 @@ var DropForeignKeyTests = []ScriptTest{
29562956
},
29572957
},
29582958
},
2959-
{
2960-
Name: "DECIMAL foreign key compatibility",
2961-
SetUpScript: []string{
2962-
"CREATE TABLE decimal_parent (d decimal(4, 2) primary key);",
2963-
"ALTER TABLE decimal_parent ADD INDEX idx_d (d);",
2964-
"INSERT INTO decimal_parent VALUES (1.23), (45.67), (78.9);",
2965-
},
2966-
Assertions: []ScriptTestAssertion{
2967-
{
2968-
Query: "CREATE TABLE decimal_child_same (d decimal(4,2), foreign key (d) references decimal_parent (d));",
2969-
Expected: []sql.Row{
2970-
{types.NewOkResult(0)},
2971-
},
2972-
},
2973-
{
2974-
Query: "INSERT INTO decimal_child_same VALUES (1.23), (45.67), (NULL);",
2975-
Expected: []sql.Row{
2976-
{types.NewOkResult(3)},
2977-
},
2978-
},
2979-
{
2980-
Query: "CREATE TABLE decimal_child_diff_scale (d decimal(4,1), foreign key (d) references decimal_parent (d));",
2981-
Expected: []sql.Row{
2982-
{types.NewOkResult(0)},
2983-
},
2984-
},
2985-
{
2986-
Query: "CREATE TABLE decimal_child_diff_precision (d decimal(3,2), foreign key (d) references decimal_parent (d));",
2987-
Expected: []sql.Row{
2988-
{types.NewOkResult(0)},
2989-
},
2990-
},
2991-
{
2992-
Query: "CREATE TABLE decimal_child_large (d decimal(65,30), foreign key (d) references decimal_parent (d));",
2993-
Expected: []sql.Row{
2994-
{types.NewOkResult(0)},
2995-
},
2996-
},
2997-
{
2998-
Query: "INSERT INTO decimal_child_diff_scale VALUES (78.9);",
2999-
ExpectedErr: sql.ErrForeignKeyChildViolation,
3000-
},
3001-
{
3002-
Query: "INSERT INTO decimal_child_diff_precision VALUES (1.23);",
3003-
Expected: []sql.Row{
3004-
{types.NewOkResult(1)},
3005-
},
3006-
},
3007-
{
3008-
Query: "INSERT INTO decimal_child_large VALUES (1.23);",
3009-
ExpectedErr: sql.ErrForeignKeyChildViolation,
3010-
},
3011-
{
3012-
Query: "INSERT INTO decimal_child_same VALUES (99.99);",
3013-
ExpectedErr: sql.ErrForeignKeyChildViolation,
3014-
},
3015-
{
3016-
Query: "INSERT INTO decimal_child_diff_scale VALUES (99.9);",
3017-
ExpectedErr: sql.ErrForeignKeyChildViolation,
3018-
},
3019-
},
3020-
},
30212959
}

enginetest/queries/script_queries.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10561,6 +10561,14 @@ where
1056110561
Query: "insert into child_dec_65_30 values (1.23);",
1056210562
ExpectedErr: sql.ErrForeignKeyChildViolation,
1056310563
},
10564+
{
10565+
Query: "insert into child_dec_4_2 values (99.99);",
10566+
ExpectedErr: sql.ErrForeignKeyChildViolation,
10567+
},
10568+
{
10569+
Query: "insert into child_dec_4_1 values (99.9);",
10570+
ExpectedErr: sql.ErrForeignKeyChildViolation,
10571+
},
1056410572
},
1056510573
},
1056610574

sql/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ var (
419419
ErrInsertIntoNonNullableProvidedNull = errors.NewKind("column name '%v' is non-nullable but attempted to set a value of null")
420420

421421
// ErrForeignKeyChildViolation is called when a rows is added but there is no parent row, and a foreign key constraint fails. Add the parent row first.
422-
ErrForeignKeyChildViolation = errors.NewKind("cannot add or update a child row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
422+
ErrForeignKeyChildViolation = errors.NewKind("cannot add or update a child row: a foreign key constraint fails (`%s`.`%s`, CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`))")
423423

424424
// ErrForeignKeyParentViolation is called when a parent row that is deleted has children, and a foreign key constraint fails. Delete the children first.
425425
ErrForeignKeyParentViolation = errors.NewKind("cannot delete or update a parent row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")

sql/plan/foreign_key_editor.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,9 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
514514
if err == nil {
515515
// We have a parent row, but for DECIMAL types we need to be strict about precision/scale
516516
if shouldReject := reference.validateDecimalMatch(ctx, row); shouldReject {
517-
return sql.ErrForeignKeyChildViolation.New(reference.ForeignKey.Name, reference.ForeignKey.Table,
518-
reference.ForeignKey.ParentTable, reference.RowMapper.GetKeyString(row))
517+
return sql.ErrForeignKeyChildViolation.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
518+
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
519+
reference.ForeignKey.ParentTable, strings.Join(reference.ForeignKey.ParentColumns, ", "))
519520
}
520521
// We have a parent row so throw no error
521522
return nil
@@ -540,8 +541,9 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
540541
}
541542
}
542543

543-
return sql.ErrForeignKeyChildViolation.New(reference.ForeignKey.Name, reference.ForeignKey.Table,
544-
reference.ForeignKey.ParentTable, reference.RowMapper.GetKeyString(row))
544+
return sql.ErrForeignKeyChildViolation.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
545+
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
546+
reference.ForeignKey.ParentTable, strings.Join(reference.ForeignKey.ParentColumns, ", "))
545547
}
546548

547549
func (reference *ForeignKeyReferenceHandler) validateDecimalMatch(ctx *sql.Context, row sql.Row) bool {

0 commit comments

Comments
 (0)