Bug 34360 - [WARN] DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results
Summary: [WARN] DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique...
Status: Pushed to main
Alias: None
Product: Koha
Classification: Unclassified
Component: Architecture, internals, and plumbing (show other bugs)
Version: Main
Hardware: All All
: P5 - low normal (vote)
Assignee: Nick Clemens (kidclamp)
QA Contact: Marcel de Rooy
URL:
Keywords: no-sandbox
Depends on: 33497
Blocks:
  Show dependency treegraph
 
Reported: 2023-07-24 14:41 UTC by Marcel de Rooy
Modified: 2024-05-10 12:12 UTC (History)
6 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Small patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
24.05.00


Attachments
Bug 34360: Get distinct biblionumber (1.93 KB, patch)
2024-01-11 12:00 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 34360: Get distinct biblionumber (1.98 KB, patch)
2024-05-09 13:53 UTC, Baptiste
Details | Diff | Splinter Review
Bug 34360: Get distinct biblionumber (2.07 KB, patch)
2024-05-10 07:24 UTC, Marcel de Rooy
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Marcel de Rooy 2023-07-24 14:41:38 UTC
Seeing this warn on current master:
[2023/07/24 13:37:03] [WARN] DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /usr/share/koha/Koha/Objects.pm line 421

Refers to sub get_column {
    my ($self, $column_name) = @_;
=>    return $self->_resultset->get_column( $column_name )->all;
}

get_column is called in the accessors of Koha/Object AUTOLOAD.

This warn must have been triggered already by doing a catalogue search in staff from the main page.
Some join or prefetch on staff detail ?
Comment 1 Marcel de Rooy 2023-07-24 14:43:06 UTC
NO
This seems to be untrue. Happily.
The accessor goes via _result.
Comment 2 Marcel de Rooy 2023-07-24 14:44:31 UTC
So could it be one of the 'genuine' calls of $self->get_column ?
Comment 3 Jonathan Druart 2024-01-10 09:24:32 UTC
This is coming from
  commit 2cf00f1a49a7b9f8d5ca105e2dd1add5d5d95198
  Bug 33497: Use 'host_items' param to fetch all items at once

-my @items = $biblio->items->search_ordered( $params )->as_list;
+my $items = $biblio->items({ host_items => 1 })->search_ordered( $params, { prefetch => ['issue','branchtransfers'] } );

And the get_column call from Koha::Items->search_ordered

453     my @biblionumbers = uniq $self->get_column('biblionumber');
Comment 4 Jonathan Druart 2024-01-10 09:37:20 UTC
I am failing at providing a correct fix. The warning looks bad, we should not ignore it.
Comment 5 Nick Clemens (kidclamp) 2024-01-11 12:00:15 UTC
Created attachment 160842 [details] [review]
Bug 34360: Get distinct biblionumber

This fixes the problem noted in the bug:
[WARN] DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /usr/share/koha/Koha/Objects.pm line 421

We are taking a list of items, assuming the scenario is that these are from a single biblio and possibly some host_items, then searching and ordering conditionally on whether the biblio is a serial.

Current code gets the first biblio from the list - this patch adds a 'DISTINCT' to the results ebfore fetching the column

To test:
1 - Find a biblio in the staff interface
2 - Transfer one of the items a few times
3 - Recreate the issue on the command line:
    export DBIC_TRACE=1
    perl -e 'use Koha::Items; my $items = Koha::Items->search({biblionumber=>9})->search_ordered(undef,{ prefetch => ['issue','current_branchtransfers'] }); $items->next'
4 - Note warning:
    DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /kohadevbox/koha/Koha/Objects.pm line 426
5 - Apply patch
6 - Repeat 3
7 - Error is gone
Comment 6 Nick Clemens (kidclamp) 2024-01-11 12:14:08 UTC
I added a patch, however, I noted a new error:
DBIx::Class::ResultSet::_construct_results(): Unable to properly collapse has_many results in iterator mode due to order criteria - performed an eager cursor slurp underneath. Consider using ->all() instead at /kohadevbox/koha/Koha/Objects.pm line 317

Maybe that's a new bug? Surely related to the branchtransfers fetch, see bug 35100


I wonder on this one though, do we need to automagically decide how to sort? 

In the contexts where we use this we are looking at a specific biblio - could we put the onus on the caller to pass in the biblio serial column? 

Every call is using: $biblio->items->search_ordered() so we have the biblio.
There is one exception in opac-detail.pl, however, it could be rewritten to use the 'host_items' option

Suggesting:
$biblio->items({search criteria})->search_ordered({ serial => $biblio->serial, other params},{});
Comment 7 Jonathan Druart 2024-01-11 14:16:19 UTC
Just a note: I am actually removing the branchtransfers join on bug 33568... Maybe it should re-introduce it (there is a FIXME there).
Comment 8 Magnus Enger 2024-02-16 14:23:48 UTC
(In reply to Nick Clemens from comment #6)
> I added a patch, however, I noted a new error:
> DBIx::Class::ResultSet::_construct_results(): Unable to properly collapse
> has_many results in iterator mode due to order criteria - performed an eager
> cursor slurp underneath. Consider using ->all() instead at
> /kohadevbox/koha/Koha/Objects.pm line 317

Should this be fixed before the current patch is signed off?
Comment 9 Marcel de Rooy 2024-02-19 07:33:34 UTC
(In reply to Nick Clemens from comment #6)
> I added a patch, however, I noted a new error:
> DBIx::Class::ResultSet::_construct_results(): Unable to properly collapse
> has_many results in iterator mode due to order criteria - performed an eager
> cursor slurp underneath. Consider using ->all() instead at
> /kohadevbox/koha/Koha/Objects.pm line 317

Have seen that one already in logs. But was hard to see where it came from exactly.
Comment 10 Baptiste 2024-05-09 13:53:26 UTC
Created attachment 166456 [details] [review]
Bug 34360: Get distinct biblionumber

This fixes the problem noted in the bug:
[WARN] DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /usr/share/koha/Koha/Objects.pm line 421

We are taking a list of items, assuming the scenario is that these are from a single biblio and possibly some host_items, then searching and ordering conditionally on whether the biblio is a serial.

Current code gets the first biblio from the list - this patch adds a 'DISTINCT' to the results ebfore fetching the column

To test:
1 - Find a biblio in the staff interface
2 - Transfer one of the items a few times
3 - Recreate the issue on the command line:
    export DBIC_TRACE=1
    perl -e 'use Koha::Items; my $items = Koha::Items->search({biblionumber=>9})->search_ordered(undef,{ prefetch => ['issue','current_branchtransfers'] }); $items->next'
4 - Note warning:
    DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /kohadevbox/koha/Koha/Objects.pm line 426
5 - Apply patch
6 - Repeat 3
7 - Error is gone

Signed-off-by: baptiste <baptiste.bayche@inlibro.com>
Comment 11 Marcel de Rooy 2024-05-10 07:23:34 UTC
(In reply to Nick Clemens (kidclamp) from comment #6)
> I added a patch, however, I noted a new error:
> DBIx::Class::ResultSet::_construct_results(): Unable to properly collapse
> has_many results in iterator mode due to order criteria - performed an eager
> cursor slurp underneath. Consider using ->all() instead at
> /kohadevbox/koha/Koha/Objects.pm line 317

I have been seeing this one regularly already on 22.11. (Perhaps on 21.11 already?) Hard to find out where it is coming from.
Comment 12 Marcel de Rooy 2024-05-10 07:24:45 UTC
Created attachment 166499 [details] [review]
Bug 34360: Get distinct biblionumber

This fixes the problem noted in the bug:
[WARN] DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /usr/share/koha/Koha/Objects.pm line 421

We are taking a list of items, assuming the scenario is that these are from a single biblio and possibly some host_items, then searching and ordering conditionally on whether the biblio is a serial.

Current code gets the first biblio from the list - this patch adds a 'DISTINCT' to the results ebfore fetching the column

To test:
1 - Find a biblio in the staff interface
2 - Transfer one of the items a few times
3 - Recreate the issue on the command line:
    export DBIC_TRACE=1
    perl -e 'use Koha::Items; my $items = Koha::Items->search({biblionumber=>9})->search_ordered(undef,{ prefetch => ['issue','current_branchtransfers'] }); $items->next'
4 - Note warning:
    DBIx::Class::ResultSetColumn::new(): Attempting to retrieve non-unique column 'biblionumber' on a resultset containing one-to-many joins will return duplicate results. at /kohadevbox/koha/Koha/Objects.pm line 426
5 - Apply patch
6 - Repeat 3
7 - Error is gone

Signed-off-by: baptiste <baptiste.bayche@inlibro.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 13 Katrin Fischer 2024-05-10 12:12:58 UTC
Pushed for 24.05!

Well done everyone, thank you!