mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Tested that get_or_create raises IntegrityError.
It used to raise "DatabaseError: no such savepoint" with the old transaction management. Closes #15117.
This commit is contained in:
		| @@ -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) | ||||
|   | ||||
| @@ -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.") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user