Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rethinkdb python handle empty result

What is the best technique with rethinkb and python to deal with empty result. I try this, but catching exceptions is not satisfactory.

@staticmethod
def get_by_mail(mail):
    try:
        return User(
            r.table('users').filter({"mail": mail}).limit(1).nth(0).run()
        )
    except RqlRuntimeError:
        return None

If anyone has tried other techniques, I am very interested. Thanks for your help.

like image 595
k3z Avatar asked Oct 02 '22 06:10

k3z


1 Answers

The easiest way to deal with this is probably by adding in the element you want back with a union.

r.table('users').filter({"mail": mail}).limit(1).union([{}])[0]

A slightly ugly work around but it should do the trick. I think we should probably extend the default syntax to work with this. I'm going to make an issue for that.

like image 170
Joe Doliner Avatar answered Oct 13 '22 00:10

Joe Doliner