Bug 41672 - Use search()->next instead of find() when using other than primary key
Summary: Use search()->next instead of find() when using other than primary key
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Architecture, internals, and plumbing (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-01-21 11:21 UTC by Johanna Räisä
Modified: 2026-01-21 13:51 UTC (History)
1 user (show)

See Also:
GIT URL:
Initiative type: ---
Sponsorship status: ---
Comma delimited list of Sponsors:
Crowdfunding goal: 0
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Johanna Räisä 2026-01-21 11:21:42 UTC
The current code uses find() to look up records with a field other than the primary key. However, find() is intended only for primary key lookups and may not work as expected with other fields.
To ensure correct and future-proof behavior, replace find() with search()->next when searching by non-primary key fields.

For example, use:
Koha::Checkouts->search({ itemnumber => $item->itemnumber })->next;
instead of:
Koha::Checkouts->find({ itemnumber => $item->itemnumber });
This change improves code reliability and compatibility with possible future schema changes.
Comment 1 Tomás Cohen Arazi (tcohen) 2026-01-21 13:51:59 UTC
(In reply to Johanna Räisä from comment #0)
> The current code uses find() to look up records with a field other than the
> primary key. However, find() is intended only for primary key lookups and
> may not work as expected with other fields.
> To ensure correct and future-proof behavior, replace find() with
> search()->next when searching by non-primary key fields.
> 
> For example, use:
> Koha::Checkouts->search({ itemnumber => $item->itemnumber })->next;
> instead of:
> Koha::Checkouts->find({ itemnumber => $item->itemnumber });
> This change improves code reliability and compatibility with possible future
> schema changes.

You are right. I made the SIP code report more reliably about empty keys, but only dealt with patron searching:

```
ILS/Patron.pm:        $patron = Koha::Patrons->find_by_identifier($patron_id);
```

I believe we should implement such methods for items/checkouts to complete the work i.e. Koha::Patrons->find_by_identifier does exactly what you are suggesting.