On Jun 17, 2013, at 08:58 , Chris Withers <ch...@simplistix.co.uk> 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.') Wichert. -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.