-
-
Notifications
You must be signed in to change notification settings - Fork 572
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
add a .first method to RecordCollections #43
Conversation
Includes #42. |
Rebased to fix a broken test in the appropriate commit, previous head was dcb9e7a. |
499fd5b allows for passing in an exception as |
Ready for review, @kennethreitz. :) |
So, I have mixed feelings about this one. Currently, you can get this behavior with I tend to lean on the side of introducing as few methods as possible to an API, and adding only what is necessary. This does seem like a good nice-to-have, though. I am torn. |
def one(self, default=None, as_dict=False, as_ordereddict=False): | ||
"""Returns a single record for the RecordCollection, or `default`.""" | ||
|
||
# Try to get a record, or return default. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be simplified to simply use use
record = self[0]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 5e60bb9.
Yes, but quite often it should be a bug if there's more than one result, and/or not a bug if there's less than one. The purpose of the
Well, it's here if you want it. :-) |
Would user = db.query('select * from users where id=1').first() I do agree that just doing |
|
Ah, gotcha. I didn't realize you were checking if there was more than one record. |
And also defaulting if there's less than one. :) |
@Brobin that was what I thought too. That's one of the main reasons I'm not gung ho about adding this — I think it's confusing :)
|
|
I think this needs a rebase if we wish to proceed ;) |
It's a common use-case to want one and only one result from a query. This adds a .one method to RecordCollections that is parallel to .all.
No CI? I will run tests locally ... |
|
|
Alright, @kennethreitz, lemme know if you want a squash or further changes or whatevs. :-) |
✨🍰✨ |
It's a common use-case to want one and only one result from a query.