#24770: Model instances are dropped by annotate when reverse foreign key is
empty.
-------------------------------------+-------------------------------------
Reporter: Ian-Foote | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ian-Foote):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> I ran across an odd interaction between `annotate`, `Sum`, `Case`, `When`
> and a reverse `ForeignKey`. I've prepared a [https://github.com/Ian-Foote
> /django-annotate-bug/blob/master/bug/tests/test_query.py#L10-L24 minimal
> testcase].
>
> The query generated by the annotate looks like:
> {{{
> SELECT "bug_post"."id", "bug_post"."created", SUM(CASE WHEN
> "bug_comment"."created" > ("bug_post"."created") THEN 1 ELSE 0 END) AS
> "unread_comments" FROM "bug_post" INNER JOIN "bug_comment" ON (
> "bug_post"."id" = "bug_comment"."post_id" ) GROUP BY "bug_post"."id",
> "bug_post"."created"
> }}}
>
> I initially ran into this on postgres, but my minimal testcase also shows
> it on sqlite.
New description:
I ran across an odd interaction between `annotate`, `Sum`, `Case`, `When`
and a reverse `ForeignKey`. I've prepared a [https://github.com/Ian-Foote
/django-annotate-bug/blob/master/bug/tests/test_query.py#L10-L24 minimal
testcase]. I expect the `Post` I created to be in the `queryset`,
annotated with `0`, but instead the `queryset` is empty.
The query generated by the annotate looks like:
{{{
SELECT "bug_post"."id", "bug_post"."created", SUM(CASE WHEN
"bug_comment"."created" > ("bug_post"."created") THEN 1 ELSE 0 END) AS
"unread_comments" FROM "bug_post" INNER JOIN "bug_comment" ON (
"bug_post"."id" = "bug_comment"."post_id" ) GROUP BY "bug_post"."id",
"bug_post"."created"
}}}
I initially ran into this on postgres, but my minimal testcase also shows
it on sqlite.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24770#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.4941c477c97e28b9969d819ec61942b0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.