Skip to content

Commit 774726d

Browse files
committed
amend test placement and decimal validation
1 parent 68264f3 commit 774726d

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

enginetest/queries/script_queries.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10529,7 +10529,10 @@ where
1052910529
{types.NewOkResult(3)},
1053010530
},
1053110531
},
10532-
10532+
{
10533+
Query: "insert into child_dec_4_2 values (99.99);",
10534+
ExpectedErr: sql.ErrForeignKeyChildViolation,
10535+
},
1053310536
{
1053410537
Query: "create table child_dec_4_1 (d decimal(4,1), foreign key (d) references parent (d));",
1053510538
Expected: []sql.Row{
@@ -10540,7 +10543,10 @@ where
1054010543
Query: "insert into child_dec_4_1 values (78.9);",
1054110544
ExpectedErr: sql.ErrForeignKeyChildViolation,
1054210545
},
10543-
10546+
{
10547+
Query: "insert into child_dec_4_1 values (99.9);",
10548+
ExpectedErr: sql.ErrForeignKeyChildViolation,
10549+
},
1054410550
{
1054510551
Query: "create table child_dec_3_2 (d decimal(3,2), foreign key (d) references parent (d));",
1054610552
Expected: []sql.Row{
@@ -10563,14 +10569,6 @@ where
1056310569
Query: "insert into child_dec_65_30 values (1.23);",
1056410570
ExpectedErr: sql.ErrForeignKeyChildViolation,
1056510571
},
10566-
{
10567-
Query: "insert into child_dec_4_2 values (99.99);",
10568-
ExpectedErr: sql.ErrForeignKeyChildViolation,
10569-
},
10570-
{
10571-
Query: "insert into child_dec_4_1 values (99.9);",
10572-
ExpectedErr: sql.ErrForeignKeyChildViolation,
10573-
},
1057410572
{
1057510573
Query: "create table child_multi_4_2_3_1 (d1 decimal(4,2), d2 decimal(3,1), foreign key (d1, d2) references parent_multi (d1, d2));",
1057610574
Expected: []sql.Row{

sql/plan/foreign_key_editor.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -548,28 +548,31 @@ func (reference *ForeignKeyReferenceHandler) validateDecimalConstraints(row sql.
548548
if reference.RowMapper.Index == nil {
549549
return nil
550550
}
551-
552551
indexColumnTypes := reference.RowMapper.Index.ColumnExpressionTypes()
553-
for i := range reference.ForeignKey.Columns {
554-
if i >= len(indexColumnTypes) {
555-
continue
552+
for parentIdx, parentCol := range indexColumnTypes {
553+
if parentIdx >= len(reference.RowMapper.IndexPositions) {
554+
break
556555
}
557-
558-
childColIdx := reference.RowMapper.IndexPositions[i]
556+
parentType := parentCol.Type
557+
childColIdx := reference.RowMapper.IndexPositions[parentIdx]
559558
childType := reference.RowMapper.SourceSch[childColIdx].Type
560-
parentType := indexColumnTypes[i].Type
561-
562-
// For decimal types, check scale compatibility (following existing pattern for type-specific validation)
563-
if childDecimal, ok := childType.(sql.DecimalType); ok {
564-
if parentDecimal, ok := parentType.(sql.DecimalType); ok {
565-
if childDecimal.Scale() != parentDecimal.Scale() {
566-
return sql.ErrForeignKeyChildViolation.New(reference.ForeignKey.Name, reference.ForeignKey.Table,
567-
reference.ForeignKey.ParentTable, reference.RowMapper.GetKeyString(row))
568-
}
569-
}
559+
childDecimal, ok := childType.(sql.DecimalType)
560+
if !ok {
561+
continue
562+
}
563+
parentDecimal, ok := parentType.(sql.DecimalType)
564+
if !ok {
565+
continue
566+
}
567+
if childDecimal.Scale() != parentDecimal.Scale() {
568+
return sql.ErrForeignKeyChildViolation.New(
569+
reference.ForeignKey.Name,
570+
reference.ForeignKey.Table,
571+
reference.ForeignKey.ParentTable,
572+
reference.RowMapper.GetKeyString(row),
573+
)
570574
}
571575
}
572-
573576
return nil
574577
}
575578

0 commit comments

Comments
 (0)