@@ -361,8 +361,6 @@ func (desc *wrapper) ValidateBackReferences(
361
361
}
362
362
switch depDesc .DescriptorType () {
363
363
case catalog .Table :
364
- // If this is a table, it may be referenced by a view, otherwise if this
365
- // is a sequence, then it may be also be referenced by a table.
366
364
vea .Report (desc .validateInboundTableRef (by , vdg ))
367
365
case catalog .Function :
368
366
// This relation may be referenced by a function.
@@ -504,49 +502,76 @@ func (desc *wrapper) validateInboundTableRef(
504
502
backReferencedTable .GetName (), backReferencedTable .GetID ())
505
503
}
506
504
if desc .IsSequence () {
507
- // The ColumnIDs field takes a different meaning when the validated
508
- // descriptor is for a sequence. In this case, they refer to the columns
509
- // in the referenced descriptor instead.
510
- for _ , colID := range by .ColumnIDs {
511
- // Skip this check if the column ID is zero. This can happen due to
512
- // bugs in 20.2.
513
- //
514
- // TODO(ajwerner): Make sure that a migration in 22.2 fixes this issue.
515
- if colID == 0 {
516
- continue
517
- }
518
- col := catalog .FindColumnByID (backReferencedTable , colID )
519
- if col == nil {
520
- return errors .AssertionFailedf ("depended-on-by relation %q (%d) does not have a column with ID %d" ,
521
- backReferencedTable .GetName (), by .ID , colID )
505
+ if err := validateSequenceColumnBackrefs (desc , backReferencedTable , by ); err != nil {
506
+ return err
507
+ }
508
+ }
509
+
510
+ // View back-references need corresponding forward reference.
511
+ if backReferencedTable .IsView () {
512
+ for _ , id := range backReferencedTable .TableDesc ().DependsOn {
513
+ if id == desc .GetID () {
514
+ return nil
522
515
}
523
- var found bool
524
- for i := 0 ; i < col .NumUsesSequences (); i ++ {
525
- if col .GetUsesSequenceID (i ) == desc .GetID () {
526
- found = true
527
- break
516
+ }
517
+ return errors .AssertionFailedf ("depended-on-by view %q (%d) has no corresponding depends-on forward reference" ,
518
+ backReferencedTable .GetName (), by .ID )
519
+ }
520
+
521
+ // Table to table back-references must have a trigger reference.
522
+ if backReferencedTable .IsTable () && desc .IsTable () {
523
+ for _ , trigger := range backReferencedTable .TableDesc ().Triggers {
524
+ for _ , id := range trigger .DependsOn {
525
+ if id == desc .GetID () {
526
+ return nil
528
527
}
529
528
}
530
- if found {
531
- continue
532
- }
533
- return errors .AssertionFailedf (
534
- "depended-on-by relation %q (%d) has no reference to this sequence in column %q (%d)" ,
535
- backReferencedTable .GetName (), by .ID , col .GetName (), col .GetID ())
536
529
}
537
- }
538
530
539
- // View back-references need corresponding forward reference.
540
- if ! backReferencedTable .IsView () {
541
- return nil
531
+ // No valid forward reference found to justify the backref.
532
+ return errors .AssertionFailedf (
533
+ "table %q (%d) does not have a forward reference to descriptor %q (%d)" ,
534
+ backReferencedTable .GetName (), by .ID , desc .GetName (), desc .GetID ())
542
535
}
543
- for _ , id := range backReferencedTable .TableDesc ().DependsOn {
544
- if id == desc .GetID () {
545
- return nil
536
+ return nil
537
+ }
538
+
539
+ func validateSequenceColumnBackrefs (
540
+ seq catalog.Descriptor ,
541
+ backReferencedTable catalog.TableDescriptor ,
542
+ by descpb.TableDescriptor_Reference ,
543
+ ) error {
544
+ // The ColumnIDs field takes a different meaning when the validated
545
+ // descriptor is for a sequence. In this case, they refer to the columns
546
+ // in the referenced descriptor instead.
547
+ for _ , colID := range by .ColumnIDs {
548
+ // Skip this check if the column ID is zero. This can happen due to
549
+ // bugs in 20.2.
550
+ //
551
+ // TODO(ajwerner): Make sure that a migration in 22.2 fixes this issue.
552
+ if colID == 0 {
553
+ continue
546
554
}
555
+ col := catalog .FindColumnByID (backReferencedTable , colID )
556
+ if col == nil {
557
+ return errors .AssertionFailedf ("depended-on-by relation %q (%d) does not have a column with ID %d" ,
558
+ backReferencedTable .GetName (), by .ID , colID )
559
+ }
560
+ var found bool
561
+ for i := 0 ; i < col .NumUsesSequences (); i ++ {
562
+ if col .GetUsesSequenceID (i ) == seq .GetID () {
563
+ found = true
564
+ break
565
+ }
566
+ }
567
+ if found {
568
+ continue
569
+ }
570
+ return errors .AssertionFailedf (
571
+ "depended-on-by relation %q (%d) has no reference to this sequence in column %q (%d)" ,
572
+ backReferencedTable .GetName (), by .ID , col .GetName (), col .GetID ())
547
573
}
548
- return errors .AssertionFailedf ("depended-on-by view %q (%d) has no corresponding depends-on forward reference" ,
549
- backReferencedTable .GetName (), by .ID )
574
+ return nil
550
575
}
551
576
552
577
// validateFK asserts that references to desc from inbound and outbound FKs are
0 commit comments