How to elegantly check the existence of an object/instance/variable and simultaneously assign it to variable if it exists in python?

Question:

I am using SQLAlchemy to populate a database and often I need to check if a orm object exists in a database before processing. This may be an unconventional question, but I found myself encountering this pattern often:

my_object = session.query(SomeObject).filter(some_fiter).first()
if my_object: # Mostly in databases...
    # Juchee it exists
    # process
else:
    # It does not exist. :-(
    my_object = SomeObject()
    # process

What I am dreaming of would be something like:

if my_object = session.query(someObject).blabla.first():
    # if my_object is None this scope is left alone
    # if my_object is not None I can work with my_object here...

I know, that this syntax is wrong, but I wanted to explain, what I mean by this example. Any equivalent way would make me happy.

Is there an elegant python approach for this pattern? This question aims not only at SQLAlchemy, but to each equivalent scenario.

closing my eyes hitting “Post your question” and waiting for the smart people and pythonistas by heart to hunt me down for asking something mayhaps inappropriate 😉

Asked By: Aufwind

||

Answers:

I know it’s not all one step, but is this acceptable?

my_object = session.query(SomeObject).filter(some_filter).first()
if my_object is None:
    my_object = SomeObject()
#process
Answered By: multipleinterfaces

wrap it on a function (shamelessly stolen from django get_or_create, this doesnt return a tuple though)

get_or_create(model, **kwargs):
    try:
        # basically check the obj from the db, this syntax might be wrong
        object = session.query(model).filter(**kwargs).first()
        return object
    except DoesNotExistException: # or whatever error/exception it is on SQLA
        object = model()
        # do it here if you want to save the obj to the db
        return object

that’s it. to use it:

obj = get_or_create(SomeObject, filters)

change the **kwargs to a simple argument (like some_filters) if you want

try to wrap something you often use (wrap them to functions or classes)

thats only pseudo code, there might be syntax error.

EDIT: emphasize

Answered By: kusut

This has been asked a long time ago but for future visitors a more concise way to check is

 if session.query(model).filter(some_filter).count():
     # do stuff
Answered By: elbear

You want to execute a Exist query to be efficient

(ret, ), = Session.query(exists().where(SomeObject.field==value))

Mike Bayer explain it in his blog post:
http://techspot.zzzeek.org/2008/09/09/selecting-booleans/

You can use scalar if you don’t want to have a tuple as result:

ret = Session.query(exists().where(SomeObject.field==value)).scalar()
Answered By: Rach
from sqlalchemy.orm.util import has_identity

my_object = session.query(SomeObject).get(id) or SomeObject()
# Processing...

# Check if the object exists in the database
if not has_identity(my_object):
    session.add(my_object)

session.commit()

.get() can be replaced with a filter() + first() if needed

Answered By: Metalstorm
if DBSession.query(ObjectType).filter(ObjectType.some_parametter == "This").first() is None:

This is an efficient one line way of checking whether a record exists. It is efficient because it only grabs the first object, and it can be on one line because first() returns None when there are no matching records. Hope that helps!

Answered By: jmercouris

Some nice suggestions here. How about using the NoResultFound exception?

try:
    existing = dbsession.query(SomeObject).filter_by(value=value).one()
    return existing
except sqlalchemy.orm.exc.NoResultFound:
    obj = SomeObject()
Answered By: suripoori

You can use this:

sth = session.query.filter_by().first()
if sth is None:
    ....
else:
    ....

I have tested it.It works well.

Answered By: bass chuck

Here is the function to check the existence of an object using SQLalchemy.

def exists(obj, **kwargs):
    """" if obj filtered by kwargs exist return it otherwise return None
        obj : is the sql alchemy model object which existence is being checked here.
        **kwargs : (username = user_name, email=user_email)
    """
    db_obj = obj.query.filter_by(**kwargs).first()
    if db_obj is not None:
        return True
    else:
        return False
Answered By: Umar Hayat
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.