`one()` and `one_or_none()` fetch all rows

Issue #3687 wontfix
Adrian created an issue

While those methods are usually used on queries which are expected to return only one row, especially one_or_none() is also a good choice when you want to ensure that you don't get more than you expected.

But if you call it e.g. on an unfiltered query on a huge table, it will load every single row even though LIMIT 2 would be enough to determine whether are there are too many rows or not.

Comments (4)

  1. Mike Bayer repo owner

    I'd prefer not to change that, LIMIT has a big change on the query especially if joined eager loading is in use, which impllies wrapping it inside of a subquery. I dont find the "load 1M rows before we raise an error" use case to be an issue in practice, you could just as easily have made the mistake of doing all() on 1M rows.

  2. Mike Bayer repo owner

    yes. first() gives you the first row in a result that can legitimately be of any size.

  3. Mike Bayer repo owner

    I'm -1 on changing any logic with one() regarding this. fetching too many rows is a mistake you can make with one() or all() in the same way.

  4. Log in to comment