Skip to content

Commit 562f30f

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 94d07f5 commit 562f30f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

sql/errors.go

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

422422
// 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.
423-
ErrForeignKeyChildViolation = errors.NewKind("cannot add or update a child row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
423+
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`))")
424424

425425
// ErrForeignKeyParentViolation is called when a parent row that is deleted has children, and a foreign key constraint fails. Delete the children first.
426426
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)