#33945: get_previous_in_order and get_next_in_order return incorrect data when objects is stored in non-default database -------------------------------------+------------------------------------- Reporter: François Granade | Owner: Wael | Ramadan Type: Bug | Status: assigned Component: Database layer | Version: 4.0 (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by Carlton Gibson):
* stage: Unreviewed => Accepted Comment: OK, I think this is valid. `Model._get_next_or_previous_in_order()` doesn't apply `using()` at all. [https://github.com/django/django/blob/e9fd2b572410b1236da0d3d0933014138d89f44e/django/db/models/base.py#L1170-L1191 src] Reproduces with the following diff: {{{ diff --git a/tests/multiple_database/models.py b/tests/multiple_database/models.py index 7de784e149..4c8bdffb96 100644 --- a/tests/multiple_database/models.py +++ b/tests/multiple_database/models.py @@ -79,3 +79,18 @@ class UserProfile(models.Model): class Meta: ordering = ("flavor",) + + +class Question(models.Model): + text = models.CharField(max_length=200) + + +class Answer(models.Model): + text = models.CharField(max_length=200) + question = models.ForeignKey(Question, models.CASCADE) + + class Meta: + order_with_respect_to = "question" + + def __str__(self): + return self.text diff --git a/tests/multiple_database/tests.py b/tests/multiple_database/tests.py index 7a9ff4da4e..8bf30c6fd9 100644 --- a/tests/multiple_database/tests.py +++ b/tests/multiple_database/tests.py @@ -12,7 +12,7 @@ from django.db.models import signals from django.db.utils import ConnectionRouter from django.test import SimpleTestCase, TestCase, override_settings -from .models import Book, Person, Pet, Review, UserProfile +from .models import Book, Person, Pet, Review, UserProfile, Question, Answer from .routers import AuthRouter, TestRouter, WriteRouter @@ -2503,6 +2503,17 @@ class RouteForWriteTestCase(TestCase): self.assertEqual(e.model, Book) self.assertEqual(e.hints, {"instance": auth}) + def test_order_with_respect_to(self): + q = Question.objects.using("other").create(text="The question") + a1 = Answer.objects.using("other").create(text="Answer 1", question=q) + a2 = Answer.objects.using("other").create(text="Answer 2", question=q) + a3 = Answer.objects.using("other").create(text="Answer 3", question=q) + q.set_answer_order([a1.pk, a2.pk, a3.pk], "other") + + a2.refresh_from_db() + self.assertIs(a2.get_next_in_order(), a3) + self.assertIs(a2.get_previous_in_order(), a1) + class NoRelationRouter: """Disallow all relations.""" }}} Let's accept to investigate. -- Ticket URL: <https://code.djangoproject.com/ticket/33945#comment:2> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/01070182c5d103c9-d616f1a9-fa48-4b13-b31a-27fffcd640f9-000000%40eu-central-1.amazonses.com.