mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Refs #26780 -- Made prefetch_related() don't use window expressions fo sliced queries if not supported.
This commit is contained in:
@@ -2,11 +2,16 @@ from unittest import mock
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db import connection
|
||||
from django.db import NotSupportedError, connection
|
||||
from django.db.models import Prefetch, QuerySet, prefetch_related_objects
|
||||
from django.db.models.query import get_prefetcher
|
||||
from django.db.models.sql import Query
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import (
|
||||
TestCase,
|
||||
override_settings,
|
||||
skipIfDBFeature,
|
||||
skipUnlessDBFeature,
|
||||
)
|
||||
from django.test.utils import CaptureQueriesContext, ignore_warnings
|
||||
from django.utils.deprecation import RemovedInDjango50Warning
|
||||
|
||||
@@ -1911,6 +1916,7 @@ class NestedPrefetchTests(TestCase):
|
||||
|
||||
|
||||
class PrefetchLimitTests(TestDataMixin, TestCase):
|
||||
@skipUnlessDBFeature("supports_over_clause")
|
||||
def test_m2m_forward(self):
|
||||
authors = Author.objects.all() # Meta.ordering
|
||||
with self.assertNumQueries(3):
|
||||
@@ -1924,6 +1930,7 @@ class PrefetchLimitTests(TestDataMixin, TestCase):
|
||||
with self.subTest(book=book):
|
||||
self.assertEqual(book.authors_sliced, list(book.authors.all())[1:])
|
||||
|
||||
@skipUnlessDBFeature("supports_over_clause")
|
||||
def test_m2m_reverse(self):
|
||||
books = Book.objects.order_by("title")
|
||||
with self.assertNumQueries(3):
|
||||
@@ -1937,6 +1944,7 @@ class PrefetchLimitTests(TestDataMixin, TestCase):
|
||||
with self.subTest(author=author):
|
||||
self.assertEqual(author.books_sliced, list(author.books.all())[1:2])
|
||||
|
||||
@skipUnlessDBFeature("supports_over_clause")
|
||||
def test_foreignkey_reverse(self):
|
||||
authors = Author.objects.order_by("-name")
|
||||
with self.assertNumQueries(3):
|
||||
@@ -1960,6 +1968,7 @@ class PrefetchLimitTests(TestDataMixin, TestCase):
|
||||
list(book.first_time_authors.all())[1:],
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature("supports_over_clause")
|
||||
def test_reverse_ordering(self):
|
||||
authors = Author.objects.reverse() # Reverse Meta.ordering
|
||||
with self.assertNumQueries(3):
|
||||
@@ -1972,3 +1981,13 @@ class PrefetchLimitTests(TestDataMixin, TestCase):
|
||||
for book in books:
|
||||
with self.subTest(book=book):
|
||||
self.assertEqual(book.authors_sliced, list(book.authors.all())[1:])
|
||||
|
||||
@skipIfDBFeature("supports_over_clause")
|
||||
def test_window_not_supported(self):
|
||||
authors = Author.objects.all()
|
||||
msg = (
|
||||
"Prefetching from a limited queryset is only supported on backends that "
|
||||
"support window functions."
|
||||
)
|
||||
with self.assertRaisesMessage(NotSupportedError, msg):
|
||||
list(Book.objects.prefetch_related(Prefetch("authors", authors[1:])))
|
||||
|
||||
Reference in New Issue
Block a user