Opened 5 years ago

Closed 5 years ago

Last modified 2 years ago

#13692 closed (duplicate)

Wrong ORM in complex filter

Reported by: myneur Owned by:
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords: filter, Q objects
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

With Models:

class Professional(models.Model):
	hits = models.PositiveIntegerField() 

class Article(models.Model):
	hits = models.PositiveIntegerField()
	authors = models.ManyToManyField(Professional)

class Query(models.Model):
	hits = models.PositiveIntegerField() 
	author = models.ForeignKey(Professional, blank = True, null = True) 

A Query:

Professional.objects.annotate(article_hits=Sum("article__hits"), query_hits=Sum("query__hits")).exclude(Q(article_hits=None)&Q(query_hits=None))

Produces exactly the same SQL as:

...exclude(Q(article_hits=None)|Q(advice_hits=None))
...filter(Q(article_hits=None)&Q(advice_hits=None))
...filter(Q(article_hits=None)|Q(advice_hits=None))

All of these queries produces a SQL with exactly the same last clause:

.... HAVING (SUM("articles_article"."hits") IS NULL AND SUM("solutions_query"."hits") IS NULL)

It doesn't respond to filter/exclude or &| operator.

(and also the result is the same)

Change History (2)

comment:1 Changed 5 years ago by glassresistor

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

This is a duplicate of http://code.djangoproject.com/ticket/11293. Well not exactly but the patch should be done together. the other has a patch will link it back here.

comment:2 Changed 2 years ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top