On Tuesday, February 2, 2016 at 2:41:22 AM UTC-5, Niphlod wrote:
>
> whatever_query.first()
> do you really want an exception raised if no results are found and write
>
> try:
>      whatever_query.one()
> except:
>      didn't find anything
>
> instead of
>
> if not whatever_query.first():
>     didn't find anything 
>

The .one method raises an exception unless there is *exactly* one matching 
record, so in web2py would be equivalent to something like:

rows = db(query).select()
if len(rows) > 1:
    raise MultipleResultsFound
elif len(rows) < 1:
    raise NoResultsFound
else:
    row = rows.first()

Not sure how useful it is, though. If you want to enforce uniqueness under 
particular conditions, you would typically do that by defining constraints 
in the database (or at least via validators) -- in other words, deal with 
it at write time rather than read time . I suppose there could also be 
cases where you don't want to enforce uniqueness but instead simply want to 
do something different depending on whether there is exactly one match -- 
but I'm not sure raising an exception would be the most straightforward 
approach in that case. It would be interesting to see some common use cases 
for the .one method.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to