Skip to content

Commit 26e0467

Browse files
committed
add versioned err
1 parent 2ed41c9 commit 26e0467

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed

enginetest/queries/script_queries.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10536,7 +10536,7 @@ where
1053610536
},
1053710537
{
1053810538
Query: "insert into child_dec_4_1 values (78.9);",
10539-
ExpectedErr: sql.ErrForeignKeyChildViolation,
10539+
ExpectedErr: sql.ErrForeignKeyChildViolationMySQL845,
1054010540
},
1054110541

1054210542
{
@@ -10559,15 +10559,15 @@ where
1055910559
},
1056010560
{
1056110561
Query: "insert into child_dec_65_30 values (1.23);",
10562-
ExpectedErr: sql.ErrForeignKeyChildViolation,
10562+
ExpectedErr: sql.ErrForeignKeyChildViolationMySQL845,
1056310563
},
1056410564
{
1056510565
Query: "insert into child_dec_4_2 values (99.99);",
10566-
ExpectedErr: sql.ErrForeignKeyChildViolation,
10566+
ExpectedErr: sql.ErrForeignKeyChildViolationMySQL845,
1056710567
},
1056810568
{
1056910569
Query: "insert into child_dec_4_1 values (99.9);",
10570-
ExpectedErr: sql.ErrForeignKeyChildViolation,
10570+
ExpectedErr: sql.ErrForeignKeyChildViolationMySQL845,
1057110571
},
1057210572
},
1057310573
},

sql/errors.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ import (
2222
"gopkg.in/src-d/go-errors.v1"
2323
)
2424

25-
// MySQL84 contains error definitions that match MySQL 8.4 behavior and formatting
26-
var MySQL84 = struct {
27-
// ErrForeignKeyChildViolation is the MySQL 8.4 compatible error format for foreign key child violations
28-
ErrForeignKeyChildViolation *errors.Kind
29-
}{
30-
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`))"),
31-
}
32-
3325
var (
3426
// ErrSyntaxError is returned when a syntax error in vitess is encountered.
3527
ErrSyntaxError = errors.NewKind("%s")
@@ -427,7 +419,8 @@ var (
427419
ErrInsertIntoNonNullableProvidedNull = errors.NewKind("column name '%v' is non-nullable but attempted to set a value of null")
428420

429421
// 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.
430-
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 - 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`))")
431424

432425
// ErrForeignKeyParentViolation is called when a parent row that is deleted has children, and a foreign key constraint fails. Delete the children first.
433426
ErrForeignKeyParentViolation = errors.NewKind("cannot delete or update a parent row - Foreign key violation on fk: `%s`, table: `%s`, referenced table: `%s`, key: `%s`")
@@ -989,10 +982,9 @@ func CastSQLError(err error) *mysql.SQLError {
989982
code = mysql.ERDupEntry
990983
case ErrPartitionNotFound.Is(err):
991984
code = 1526 // TODO: Needs to be added to vitess
985+
case ErrForeignKeyChildViolationMySQL845.Is(err):
992986
case ErrForeignKeyChildViolation.Is(err):
993987
code = mysql.ErNoReferencedRow2 // test with mysql returns 1452 vs 1216
994-
case MySQL84.ErrForeignKeyChildViolation.Is(err):
995-
code = mysql.ErNoReferencedRow2 // test with mysql returns 1452 vs 1216
996988
case ErrForeignKeyParentViolation.Is(err):
997989
code = mysql.ERRowIsReferenced2 // test with mysql returns 1451 vs 1215
998990
case ErrDuplicateEntry.Is(err):

sql/plan/foreign_key_editor.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,8 @@ 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-
// Use MySQL 8.4 compatible format for DECIMAL foreign key violations
518-
return sql.MySQL84.ErrForeignKeyChildViolation.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
519-
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
517+
return sql.ErrForeignKeyChildViolationMySQL845.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
518+
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
520519
reference.ForeignKey.ParentTable, strings.Join(reference.ForeignKey.ParentColumns, ", "))
521520
}
522521
// We have a parent row so throw no error
@@ -542,9 +541,8 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
542541
}
543542
}
544543

545-
// Use MySQL 8.4 compatible format for all foreign key violations going forward
546-
return sql.MySQL84.ErrForeignKeyChildViolation.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
547-
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
544+
return sql.ErrForeignKeyChildViolationMySQL845.New(reference.ForeignKey.Database, reference.ForeignKey.Table,
545+
reference.ForeignKey.Name, strings.Join(reference.ForeignKey.Columns, ", "),
548546
reference.ForeignKey.ParentTable, strings.Join(reference.ForeignKey.ParentColumns, ", "))
549547
}
550548

@@ -624,7 +622,7 @@ func (mapper *ForeignKeyRowMapper) GetIter(ctx *sql.Context, row sql.Row, refChe
624622
}
625623

626624
targetType := mapper.SourceSch[rowPos].Type
627-
625+
628626
// Transform the type of the value in this row to the one in the other table for the index lookup, if necessary
629627
if mapper.TargetTypeConversions != nil && mapper.TargetTypeConversions[rowPos] != nil {
630628
var err error
@@ -748,7 +746,6 @@ func GetForeignKeyTypeConversions(
748746
return nil, nil
749747
}
750748

751-
752749
fromType := childExtendedType
753750
toType := parentExtendedType
754751
if direction == ParentToChild {

0 commit comments

Comments
 (0)