Skip to content

Commit

Permalink
revise IntegrityError handling (#1709)
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Jan 16, 2025
1 parent e54e0b1 commit a965ff6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions geniza/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.validators import RegexValidator
from django.db import IntegrityError, models
from django.db import IntegrityError, models, transaction
from django.db.models import F, Q, Value
from django.db.models.query import Prefetch
from django.forms import ValidationError
Expand Down Expand Up @@ -1075,12 +1075,15 @@ def merge_with(self, merge_relation_types, user=None):

# combine person-document relationships
for relationship in rel_type.persondocumentrelation_set.all():
# set type of each relationship to primary relation type
relationship.type = self
# handle unique constraint violation (one relationship per type
# between doc and person): only reassign type if it doesn't
# create a duplicate, otherwise delete.
# see https://docs.djangoproject.com/en/3.2/topics/db/transactions/#django.db.transaction.atomic
try:
relationship.save()
with transaction.atomic():
relationship.save()
except IntegrityError:
relationship.delete()

Expand Down
2 changes: 1 addition & 1 deletion geniza/entities/tests/test_entities_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def test_str(self):
assert str(relation) == f"{recipient} relation: {goitein} and {doc}"


@pytest.mark.django_db(transaction=True)
@pytest.mark.django_db
class TestPersonDocumentRelationType:
def test_merge_with(self, person, person_multiname, document, join):
# create two PersonDocumentRelationTypes and some associations
Expand Down

0 comments on commit a965ff6

Please sign in to comment.