Bug 31133: Don't generate values for more than one FK
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 12 Jul 2022 07:11:12 +0000 (09:11 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 12 Jul 2022 13:57:52 +0000 (10:57 -0300)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
t/lib/TestBuilder.pm

index 94ffa97..28a55fd 100644 (file)
@@ -123,9 +123,16 @@ sub build {
     # loop thru all fk and create linked records if needed
     # fills remaining entries in $col_values
     my $foreign_keys = $self->_getForeignKeys( { source => $source } );
+    my $col_names = {};
     for my $fk ( @$foreign_keys ) {
         # skip when FK points to itself: e.g. borrowers:guarantorid
         next if $fk->{source} eq $source;
+
+        # If we have more than one FK on the same column, we only generate values for the first one
+        next
+          if scalar @{ $fk->{keys} } == 1
+          && exists $col_names->{ $fk->{keys}->[0]->{col_name} };
+
         my $keys = $fk->{keys};
         my $tbl = $fk->{source};
         my $res = $self->_create_links( $tbl, $keys, $col_values, $value );
@@ -133,6 +140,9 @@ sub build {
         foreach( keys %$res ) { # save new values
             $col_values->{$_} = $res->{$_};
         }
+
+        $col_names->{ $fk->{keys}->[0]->{col_name} } = 1
+          if scalar @{ $fk->{keys} } == 1
     }
 
     # store this record and return hashref