Opened 6 years ago

Closed 6 years ago

Last modified 2 years ago

#10916 closed (duplicate)

annotate() method doesn't include content_type_id into WHERE clause when used with generic relations

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

Description

Content type must be included to WHERE clause to prevent object_id's collisions.

>>> qs = Car.objects.annotate(gallery__count=Count('galleries')).order_by('-gallery__count')
>>> qs.query.as_sql()
('SELECT `cars_car`.`id`, `cars_car`.`brand_id`, `cars_car`.`name`, `cars_car`.`year`, `cars_car`.`slug`, 
 `cars_car`.`image`, `cars_car`.`description`, COUNT(`gallery_gallery`.`id`) AS `gallery__count` 
 FROM `cars_car` LEFT OUTER JOIN `gallery_gallery` ON (`cars_car`.`id` = `gallery_gallery`.`object_id`) 
 GROUP BY `cars_car`.`id` ORDER BY gallery__count DESC', ())

Change History (3)

comment:1 Changed 6 years ago by Alex

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

Dupe of #10870

comment:2 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

comment:3 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