diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py index 678f5a401c..82905de4f8 100644 --- a/tests/get_or_create/models.py +++ b/tests/get_or_create/models.py @@ -24,3 +24,7 @@ class Person(models.Model): class ManualPrimaryKeyTest(models.Model): id = models.IntegerField(primary_key=True) data = models.CharField(max_length=100) + + +class Profile(models.Model): + person = models.ForeignKey(Person, primary_key=True) diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 1e300fbb4d..e9cce9bbde 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -4,9 +4,9 @@ from datetime import date import traceback from django.db import IntegrityError -from django.test import TestCase +from django.test import TestCase, TransactionTestCase -from .models import Person, ManualPrimaryKeyTest +from .models import Person, ManualPrimaryKeyTest, Profile class GetOrCreateTests(TestCase): @@ -64,3 +64,16 @@ class GetOrCreateTests(TestCase): formatted_traceback = traceback.format_exc() self.assertIn('obj.save', formatted_traceback) + +class GetOrCreateTransactionTests(TransactionTestCase): + + def test_get_or_create_integrityerror(self): + # Regression test for #15117. Requires a TransactionTestCase on + # databases that delay integrity checks until the end of transactions, + # otherwise the exception is never raised. + try: + Profile.objects.get_or_create(person=Person(id=1)) + except IntegrityError: + pass + else: + self.skipTest("This backend does not support integrity checks.")