summaryrefslogtreecommitdiffstats
path: root/Server/bkr/server/model.py
diff options
context:
space:
mode:
Diffstat (limited to 'Server/bkr/server/model.py')
-rw-r--r--Server/bkr/server/model.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/Server/bkr/server/model.py b/Server/bkr/server/model.py
index 637036e..88d3a80 100644
--- a/Server/bkr/server/model.py
+++ b/Server/bkr/server/model.py
@@ -15,7 +15,7 @@ from sqlalchemy.orm import relation, backref, synonym, dynamic_loader, \
from sqlalchemy.orm.interfaces import AttributeExtension
from sqlalchemy.sql import exists
from sqlalchemy.sql.expression import join
-from sqlalchemy.exceptions import InvalidRequestError
+from sqlalchemy.exceptions import InvalidRequestError, IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from identity import LdapSqlAlchemyIdentityProvider
from cobbler_utils import consolidate, string_to_hash
@@ -1200,13 +1200,17 @@ class MappedObject(object):
@classmethod
def lazy_create(cls, **kwargs):
- item = None
+ """
+ Returns the instance identified by the given uniquely-identifying
+ attributes. If it doesn't exist yet, it is inserted first.
+ """
+ session.begin_nested()
try:
- item = cls.query.filter_by(**kwargs).one()
- except NoResultFound:
item = cls(**kwargs)
- session.add(item)
- session.flush([item])
+ session.commit()
+ except IntegrityError:
+ session.rollback()
+ item = cls.query.filter_by(**kwargs).one()
return item
def __init__(self, **kwargs):