-
-
Save rmoorman/8693030 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from sqlalchemy.exc import IntegrityError | |
from sqlalchemy.sql.expression import ClauseElement | |
def _get_or_create(session, model, defaults=None, **kwargs): | |
try: | |
query = session.query(model).filter_by(**kwargs) | |
instance = query.first() | |
if instance: | |
return instance, False | |
else: | |
session.begin(nested=True) | |
try: | |
params = dict((k, v) for k, v in kwargs.iteritems() if not isinstance(v, ClauseElement)) | |
params.update(defaults) | |
instance = model(**params) | |
session.add(instance) | |
session.commit() | |
return instance, True | |
except IntegrityError as e: | |
session.rollback() | |
instance = query.one() | |
return instance, False | |
except Exception as e: | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment