Skip to content

Commit

Permalink
Fix BulkCreateManager.bulk_get_or_create()
Browse files Browse the repository at this point in the history
closes #4306
  • Loading branch information
jortel committed Jan 8, 2019
1 parent 5876f37 commit f4eb840
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pulpcore/app/models/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hashlib

from django.core import validators
from django.db import IntegrityError, models
from django.db import IntegrityError, models, transaction
from django.forms.models import model_to_dict

from itertools import chain
Expand Down Expand Up @@ -39,19 +39,21 @@ def bulk_get_or_create(self, objs, batch_size=None):
"""
objs = list(objs)
try:
return super().bulk_create(objs, batch_size=batch_size)
with transaction.atomic():
return super().bulk_create(objs, batch_size=batch_size)
except IntegrityError:
for i in range(len(objs)):
try:
objs[i].save()
with transaction.atomic():
objs[i].save()
except IntegrityError:
objs[i] = objs[i].__class__.objects.get(objs[i].q())
return objs


class QueryMixin():
class QueryMixin:
"""
A mixin that provides models with querying utilities
A mixin that provides models with querying utilities.
"""

def q(self):
Expand Down

0 comments on commit f4eb840

Please sign in to comment.