@@ -522,7 +522,7 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
522
522
if validationErr := reference .validateColumnTypeConstraints (ctx , row , parentRow ); validationErr != nil {
523
523
return validationErr
524
524
}
525
-
525
+
526
526
// We have a parent row so throw no error
527
527
return nil
528
528
}
@@ -550,33 +550,32 @@ func (reference *ForeignKeyReferenceHandler) CheckReference(ctx *sql.Context, ro
550
550
reference .ForeignKey .ParentTable , reference .RowMapper .GetKeyString (row ))
551
551
}
552
552
553
-
554
553
// validateColumnTypeConstraints validates that column types meet MySQL foreign key requirements.
555
554
// Centralizes type validation for decimal scale matching and exact time type precision matching.
556
555
func (reference * ForeignKeyReferenceHandler ) validateColumnTypeConstraints (ctx * sql.Context , childRow sql.Row , parentRow sql.Row ) error {
557
556
mapper := reference .RowMapper
558
557
if mapper .Index == nil {
559
558
return nil
560
559
}
561
-
560
+
562
561
for parentIdx , parentCol := range mapper .Index .ColumnExpressionTypes () {
563
562
if parentIdx >= len (mapper .IndexPositions ) {
564
563
break
565
564
}
566
-
565
+
567
566
parentType := parentCol .Type
568
567
childType := mapper.SourceSch [mapper.IndexPositions [parentIdx ]].Type
569
-
568
+
570
569
// Check for constraint violations
571
570
hasViolation := false
572
-
571
+
573
572
// Decimal scale must match
574
573
if childDecimal , ok := childType .(sql.DecimalType ); ok {
575
574
if parentDecimal , ok := parentType .(sql.DecimalType ); ok {
576
575
hasViolation = childDecimal .Scale () != parentDecimal .Scale ()
577
576
}
578
577
}
579
-
578
+
580
579
// Time types must match exactly (including precision)
581
580
if ! hasViolation {
582
581
isChildTime := types .IsTime (childType ) || types .IsTimespan (childType )
@@ -585,7 +584,7 @@ func (reference *ForeignKeyReferenceHandler) validateColumnTypeConstraints(ctx *
585
584
hasViolation = ! childType .Equals (parentType )
586
585
}
587
586
}
588
-
587
+
589
588
if hasViolation {
590
589
return sql .ErrForeignKeyChildViolation .New (
591
590
reference .ForeignKey .Name ,
@@ -598,7 +597,6 @@ func (reference *ForeignKeyReferenceHandler) validateColumnTypeConstraints(ctx *
598
597
return nil
599
598
}
600
599
601
-
602
600
// CheckTable checks that every row in the table has an index entry in the referenced table.
603
601
func (reference * ForeignKeyReferenceHandler ) CheckTable (ctx * sql.Context , tbl sql.ForeignKeyTable ) error {
604
602
partIter , err := tbl .Partitions (ctx )
@@ -656,7 +654,7 @@ func (mapper *ForeignKeyRowMapper) GetIter(ctx *sql.Context, row sql.Row, refChe
656
654
}
657
655
658
656
targetType := mapper .SourceSch [rowPos ].Type
659
-
657
+
660
658
// Transform the type of the value in this row to the one in the other table for the index lookup, if necessary
661
659
if mapper .TargetTypeConversions != nil && mapper .TargetTypeConversions [rowPos ] != nil {
662
660
var err error
0 commit comments