Skip to content

Commit

Permalink
Try to add model first
Browse files Browse the repository at this point in the history
  • Loading branch information
nadzyah committed Aug 11, 2023
1 parent df747ba commit 2049940
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions backend/test_observer/data_access/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from sqlalchemy import and_, func
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.exc import IntegrityError

from .models_enums import FamilyName
from .models import DataModel, Family, Stage, Artefact
Expand Down Expand Up @@ -114,9 +115,12 @@ def get_or_create(
creation_kwargs = creation_kwargs or {}
instance = model(**filter_kwargs, **creation_kwargs)

db_instance = db.query(model).filter_by(**filter_kwargs).one_or_none()
if db_instance is None:
try:
# Attempt to add and commit the new instance
db.add(instance)
db.commit()
return instance
return db_instance
except Exception:
# Query and return the existing instance
return db.query(model).filter_by(**filter_kwargs).one()

0 comments on commit 2049940

Please sign in to comment.