@@ -, +, @@ TestBuilder ->single at /kohadevbox/koha/t/lib/TestBuilder.pm line 235 $ prove t/db_dependent --- t/db_dependent/TestBuilder.t | 23 ++++++++++++++++++++++- t/lib/TestBuilder.pm | 9 ++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) --- a/t/db_dependent/TestBuilder.t +++ a/t/db_dependent/TestBuilder.t @@ -21,7 +21,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 14; +use Test::More tests => 15; use Test::Warn; use File::Basename qw(dirname); @@ -477,3 +477,24 @@ subtest 'build_sample_biblio() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'Existence of object is only checked using primary keys' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + warnings_are { + $builder->build_object({ + class => 'Koha::Holds', + value => { + biblionumber => $biblio->biblionumber + } + }); + } [], "No warning about query returning more than one row"; + + $schema->storage->txn_rollback; +}; --- a/t/lib/TestBuilder.pm +++ a/t/lib/TestBuilder.pm @@ -231,8 +231,15 @@ sub _create_links { if( $cnt_scalar == @$keys ) { # if one or more fk cols are null, the FK constraint will not be forced return {} if $cnt_null > 0; + # does the record exist already? - return {} if $self->schema->resultset($linked_tbl)->find( $fk_value ); + my @pks = $self->schema->source( $linked_tbl )->primary_columns; + my %fk_pk_value; + for (@pks) { + $fk_pk_value{$_} = $fk_value->{$_} if defined $fk_value->{$_}; + } + return {} if !(keys %fk_pk_value); + return {} if $self->schema->resultset($linked_tbl)->find( \%fk_pk_value ); } # create record with a recursive build call my $row = $self->build({ source => $linked_tbl, value => $fk_value }); --