mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #25544 -- Removed duplicate ids in prefetch_related() queries.
This commit is contained in:
@@ -6,6 +6,7 @@ from django.db import connection
|
||||
from django.db.models import Prefetch
|
||||
from django.db.models.query import get_prefetcher
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
from django.utils import six
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
@@ -245,6 +246,27 @@ class PrefetchRelatedTests(TestCase):
|
||||
# save of reverse relation assignment.
|
||||
self.assertEqual(self.author1.books.count(), 2)
|
||||
|
||||
def test_m2m_then_reverse_fk_object_ids(self):
|
||||
with CaptureQueriesContext(connection) as queries:
|
||||
list(Book.objects.prefetch_related('authors__addresses'))
|
||||
|
||||
sql = queries[-1]['sql']
|
||||
self.assertEqual(sql.count(self.author1.name), 1)
|
||||
|
||||
def test_m2m_then_m2m_object_ids(self):
|
||||
with CaptureQueriesContext(connection) as queries:
|
||||
list(Book.objects.prefetch_related('authors__favorite_authors'))
|
||||
|
||||
sql = queries[-1]['sql']
|
||||
self.assertEqual(sql.count(self.author1.name), 1)
|
||||
|
||||
def test_m2m_then_reverse_one_to_one_object_ids(self):
|
||||
with CaptureQueriesContext(connection) as queries:
|
||||
list(Book.objects.prefetch_related('authors__authorwithage'))
|
||||
|
||||
sql = queries[-1]['sql']
|
||||
self.assertEqual(sql.count(str(self.author1.id)), 1, sql)
|
||||
|
||||
|
||||
class CustomPrefetchTests(TestCase):
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user