#36029: Deep conditions in FilteredRelation raise ProgrammingError if annotation
reused
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.1
  (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
-------------------------------------+-------------------------------------
Description changed by Jacob Walls:

Old description:

> I was debugging a query that failed like this:
> {{{#!py
> psycopg.errors.UndefinedTable: missing FROM-clause entry for table
> "book_editor"
> LINE 1: ...r" LEFT OUTER JOIN "filtered_relation_editor" ON
> (book_edito...
> }}}
>
> #33929 seems related. Here, the key is using a deeper condition relation
> (`book__editor`) than the filtered relation (`book`) and then reusing
> that attempted annotation in another `annotate()`.
>
> Failing unit test. The test right above
> [https://github.com/django/django/blob/f05edb2b43c347d4929efd52c8e2b4e08839f542/tests/filtered_relation/tests.py#L624-L635
> asserts] that a ValueError is raised in a similar situation, so maybe
> this test should do that also?
>
> {{{#!diff
> diff --git a/tests/filtered_relation/tests.py
> b/tests/filtered_relation/tests.py
> index 82caba8662..e15efb447b 100644
> --- a/tests/filtered_relation/tests.py
> +++ b/tests/filtered_relation/tests.py
> @@ -668,6 +668,15 @@ class FilteredRelationTests(TestCase):
>                  ),
>              )
>
> +    def test_condition_deeper_relation_name_reused_annotation(self):
> +        qs = Author.objects.annotate(
> +            book_editor=FilteredRelation(
> +                "book",
> +                condition=Q(book__editor__name="b"),
> +            ),
> +        ).annotate(reused_annotation=F("book_editor"))
> +        self.assertEqual(qs.count(), 2)
> +
>      def test_with_empty_relation_name_error(self):
>          with self.assertRaisesMessage(ValueError, "relation_name cannot
> be empty."):
>              FilteredRelation("", condition=Q(blank=""))
> }}}

New description:

 I was debugging a query that failed like this:
 {{{#!py
 psycopg.errors.UndefinedTable: missing FROM-clause entry for table
 "book_editor"
 LINE 1: ...r" LEFT OUTER JOIN "filtered_relation_editor" ON (book_edito...
 }}}

 #34229 seems related. Here, the key is using a deeper condition relation
 (`book__editor`) than the filtered relation (`book`) and then reusing that
 attempted annotation in another `annotate()`.

 Failing unit test. The test right above
 
[https://github.com/django/django/blob/f05edb2b43c347d4929efd52c8e2b4e08839f542/tests/filtered_relation/tests.py#L624-L635
 asserts] that a ValueError is raised in a similar situation, so maybe this
 test should do that also? See comment:1

 {{{#!diff
 diff --git a/tests/filtered_relation/tests.py
 b/tests/filtered_relation/tests.py
 index 82caba8662..e15efb447b 100644
 --- a/tests/filtered_relation/tests.py
 +++ b/tests/filtered_relation/tests.py
 @@ -668,6 +668,15 @@ class FilteredRelationTests(TestCase):
                  ),
              )

 +    def test_condition_deeper_relation_name_reused_annotation(self):
 +        qs = Author.objects.annotate(
 +            book_editor=FilteredRelation(
 +                "book",
 +                condition=Q(book__editor__name="b"),
 +            ),
 +        ).annotate(reused_annotation=F("book_editor"))
 +        self.assertEqual(qs.count(), 2)
 +
      def test_with_empty_relation_name_error(self):
          with self.assertRaisesMessage(ValueError, "relation_name cannot
 be empty."):
              FilteredRelation("", condition=Q(blank=""))
 }}}

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36029#comment:3>
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 visit 
https://groups.google.com/d/msgid/django-updates/01070193e5155a8c-87a2dd17-3e92-4342-90c7-6502286d0c45-000000%40eu-central-1.amazonses.com.

Reply via email to