Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make QueryBuilder.one return single value instead of list if only single projection #3946

Open
sphuber opened this issue Apr 19, 2020 · 3 comments

Comments

@sphuber
Copy link
Contributor

sphuber commented Apr 19, 2020

When projecting a single property in a query, one() and first() will still return a list, forcing one to still index the returned list to get the value of interest. This leads to a lot of occurrences of the following code:

result = builder.one()[0]

If there is only a single projection in the query, then the QueryBuilder knows that the user most likely just wants the single result and could return that instead of the pointless list. This would simplify the code to

result = builder.one()

The difference in type of the result value is acceptable, I find, because the caller defines the projections and so is guaranteed to know what the type of the return value is going to be. However, this would be a backwards incompatible change, so this could only be integrated in v2.0.0 at the earliest.

@chrisjsewell
Copy link
Member

chrisjsewell commented Aug 24, 2021

completely agree, @sphuber for back-compatibility, perhaps first we could add an option like:

result = builder.one(True)

single (or another name?) would be None by default, but would emit a warning if not set; that eventually single=True will be the default.
does that make sense? e.g.

def one(single: Optional[bool] = None):
    if single is None:
       warn('`single` option not set, defaulting to False, in v3 this will default to True')
       single = False
    ...
    if single:
       return result[0]

@sphuber
Copy link
Contributor Author

sphuber commented Aug 24, 2021

Would be great to add this and have a deprecation pathway as you suggested. Maybe just a different name. Something like unpack? Since that is the name for unpacking a tuple into independent variables no?

@sphuber sphuber added this to the v2.0.0 milestone Aug 24, 2021
@sphuber sphuber added type/accepted feature approved feature request and removed type/feature request status undecided labels Aug 24, 2021
@sphuber sphuber modified the milestones: v2.0.0, Post 2.0 Sep 22, 2021
@sphuber sphuber changed the title Make one() and first() methods of QueryBuilder return single value instead of list if only single projection Make QueryBuilder.one return single value instead of list if only single projection Apr 27, 2022
@sphuber
Copy link
Contributor Author

sphuber commented Apr 27, 2022

This was added for first in #5410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants