#35356: Issue with OneToOneField and recursive relationships in select_related()
and only().
-------------------------------------+-------------------------------------
     Reporter:  Joshua van Besouw    |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.2
  (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 Simon Charette):

 * stage:  Unreviewed => Accepted

Comment:

 {{{!diff
 diff --git a/tests/defer_regress/models.py b/tests/defer_regress/models.py
 index dd492993b7..38ba4a622f 100644
 --- a/tests/defer_regress/models.py
 +++ b/tests/defer_regress/models.py
 @@ -10,6 +10,12 @@ class Item(models.Model):
      text = models.TextField(default="xyzzy")
      value = models.IntegerField()
      other_value = models.IntegerField(default=0)
 +    source = models.OneToOneField(
 +        "self",
 +        related_name="destination",
 +        on_delete=models.CASCADE,
 +        null=True,
 +    )


  class RelatedItem(models.Model):
 diff --git a/tests/defer_regress/tests.py b/tests/defer_regress/tests.py
 index 10100e348d..d8b2186f0a 100644
 --- a/tests/defer_regress/tests.py
 +++ b/tests/defer_regress/tests.py
 @@ -309,6 +309,21 @@ def test_only_reverse_many_to_many_ignored(self):
          with self.assertNumQueries(1):
              self.assertEqual(Item.objects.only("request").get(), item)

 +    def test_self_referential(self):
 +        first = Item.objects.create(name="first", value=1)
 +        second = Item.objects.create(name="second", value=2,
 source=first)
 +        with self.assertNumQueries(1):
 +            deferred_first, deferred_second = (
 +                Item.objects.select_related("source", "destination")
 +                .only("name", "source__name", "destination__value")
 +                .order_by("pk")
 +            )
 +        with self.assertNumQueries(0):
 +            self.assertEqual(deferred_first.name, first.name)
 +            self.assertEqual(deferred_second.name, second.name)
 +            self.assertEqual(deferred_second.source.name, first.name)
 +            self.assertEqual(deferred_first.destination.name,
 second.name)
 +

  class DeferDeletionSignalsTests(TestCase):
      senders = [Item, Proxy]
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35356#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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018eb1049105-540492a3-8c8b-4fe9-9fff-62469b39c21f-000000%40eu-central-1.amazonses.com.

Reply via email to