Skip to content

Commit 27c16cf

Browse files
elianddbclaude
andcommitted
Revert to old error and remove MySQL 8.4.5 error
- Use original ErrForeignKeyChildViolation error instead of MySQL 8.4.5 specific error - Remove ErrForeignKeyChildViolationMySQL845 from error definitions and case statements - Update tests to use old error - Fix error message to include proper row data by passing row to validateDecimalConstraints - Ensure error messages contain correct foreign key violation information 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c213312 commit 27c16cf

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

enginetest/queries/script_queries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10538,7 +10538,7 @@ where
1053810538
},
1053910539
{
1054010540
Query: "insert into child_dec_4_1 values (78.9);",
10541-
ExpectedErr: sql.ErrForeignKeyChildViolationMySQL845,
10541+
ExpectedErr: sql.ErrForeignKeyChildViolation,
1054210542
},
1054310543

1054410544
{
@@ -10561,7 +10561,7 @@ where
1056110561
},
1056210562
{
1056310563
Query: "insert into child_dec_65_30 values (1.23);",
10564-
ExpectedErr: sql.ErrForeignKeyChildViolationMySQL845,
10564+
ExpectedErr: sql.ErrForeignKeyChildViolation,
1056510565
},
1056610566
{
1056710567
Query: "insert into child_dec_4_2 values (99.99);",

sql/errors.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +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`")
423-
ErrForeignKeyChildViolationMySQL845 = errors.NewKind("cannot add or update a child row: a foreign key constraint fails (`%s`.`%s`, CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`))")
422+
ErrForeignKeyChildViolation = errors.NewKind("cannot add or update a child row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
424423

425424
// ErrForeignKeyParentViolation is called when a parent row that is deleted has children, and a foreign key constraint fails. Delete the children first.
426425
ErrForeignKeyParentViolation = errors.NewKind("cannot delete or update a parent row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
@@ -982,7 +981,6 @@ func CastSQLError(err error) *mysql.SQLError {
982981
code = mysql.ERDupEntry
983982
case ErrPartitionNotFound.Is(err):
984983
code = 1526 // TODO: Needs to be added to vitess
985-
case ErrForeignKeyChildViolationMySQL845.Is(err):
986984
case ErrForeignKeyChildViolation.Is(err):
987985
code = mysql.ErNoReferencedRow2 // test with mysql returns 1452 vs 1216
988986
case ErrForeignKeyParentViolation.Is(err):

sql/plan/foreign_key_editor.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
513513
}
514514
if err == nil {
515515
// We have a parent row, but check for type-specific validation
516-
if validationErr := reference.validateDecimalConstraints(); validationErr != nil {
516+
if validationErr := reference.validateDecimalConstraints(row); validationErr != nil {
517517
return validationErr
518518
}
519519
// We have a parent row so throw no error
@@ -544,7 +544,7 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
544544
}
545545

546546
// validateDecimalConstraints checks that decimal foreign key columns have compatible scales.
547-
func (reference *ForeignKeyReferenceHandler) validateDecimalConstraints() error {
547+
func (reference *ForeignKeyReferenceHandler) validateDecimalConstraints(row sql.Row) error {
548548
if reference.RowMapper.Index == nil {
549549
return nil
550550
}
@@ -563,9 +563,8 @@ func (reference *ForeignKeyReferenceHandler) validateDecimalConstraints() error
563563
if childDecimal, ok := childType.(sql.DecimalType); ok {
564564
if parentDecimal, ok := parentType.(sql.DecimalType); ok {
565565
if childDecimal.Scale() != parentDecimal.Scale() {
566-
return sql.ErrForeignKeyChildViolationMySQL845.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
567-
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
568-
reference.ForeignKey.ParentTable, strings.Join(reference.ForeignKey.ParentColumns, ", "))
566+
return sql.ErrForeignKeyChildViolation.New(reference.ForeignKey.Name, reference.ForeignKey.Table,
567+
reference.ForeignKey.ParentTable, reference.RowMapper.GetKeyString(row))
569568
}
570569
}
571570
}

0 commit comments

Comments
 (0)