>From the docs:
"one()

Return exactly one result or raise an exception.

Raises sqlalchemy.orm.exc.NoResultFound if the query selects no rows.

Raises sqlalchemy.orm.exc.MultipleResultsFound if multiple object
identities are returned, or if multiple rows are returned for a query
that does not return object identities."

Thus, you could:

try:
  return query.one()
  print ('Yay! One result!')
except NoResultFound:
  # Deal with case of zero results
  print('Zero results is also good!')
except MultipleResultsFound:
  # Deal with case of >1 results
  print('This should not happen. :(')



On Mon, Jun 17, 2013 at 1:57 PM, Petr Viktorin <[email protected]> wrote:
> Simply handling NoResultFound should work just fine...
>
> def zero_or_one(query):
>     try:
>         return query.one()
>     except NoResultFound:
>         return None
>
> On Mon, Jun 17, 2013 at 6:36 PM, Michael Bayer <[email protected]> 
> wrote:
>>
>> On Jun 17, 2013, at 5:33 AM, Wichert Akkerman <[email protected]> wrote:
>>
>>>
>>> On Jun 17, 2013, at 08:58 , Chris Withers <[email protected]> wrote:
>>>
>>>> Hi All,
>>>>
>>>> I seems to commonly need to do a query which should return zero or one 
>>>> mapped objects.
>>>>
>>>> .one() isn't what I want as no returned object is ok.
>>>>
>>>> .first() isn't what I want as if my query would return more than one 
>>>> object, I have the query wrong and so want an exception.
>>>>
>>>> Is there something already available that meets this need?
>>>
>>> This will requrie you to run a query which limits the result to max 2 rows 
>>> so you can check if more than one result would be returned. I would just 
>>> create a simple wrapper function:
>>>
>>>
>>> def one_optional(query):
>>>    rows = query.limit(2).all()
>>>    count = len(rows)
>>>    if count == 0:
>>>        return None
>>>    elif count == 1:
>>>        return rows[0]
>>>    else:
>>>        raise RuntimeError('More than one result found.')
>>
>> you could even drop the limit(2) so that the query renders more simply if 
>> eagerloads are present.
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "sqlalchemy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



-- 
Ian Marcinkowski
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to