Skip to content

Commit

Permalink
Attempt final get() when IntegrityError is raised during get_or_create
Browse files Browse the repository at this point in the history
  • Loading branch information
jleclanche committed Jan 9, 2018
1 parent daef6b5 commit 41c5692
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import stripe
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models, transaction
from django.db import IntegrityError, models, transaction
from django.db.models.deletion import SET_NULL
from django.db.models.fields import BooleanField, CharField, DateTimeField, UUIDField
from django.db.models.fields.related import ForeignKey, OneToOneField
Expand Down Expand Up @@ -347,7 +347,10 @@ def _get_or_create_from_stripe_object(cls, data, field_name="id", refetch=True,
# If this happens when syncing Stripe data, it's a djstripe bug. Report it!
assert not should_expand, "No data to create {} from {}".format(cls.__name__, field_name)

return cls._create_from_stripe_object(data, save=save), True
try:
return cls._create_from_stripe_object(data, save=save), True
except IntegrityError:
return cls.stripe_objects.get(stripe_id=stripe_id), False

@classmethod
def _stripe_object_to_customer(cls, target_cls, data):
Expand Down

0 comments on commit 41c5692

Please sign in to comment.