Opened 3 years ago
Closed 3 years ago
#33775 closed Bug (invalid)
subquery must return only one column
Reported by: | Mohammad Ali Mehdizadeh | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have two tables:
class Product(models.Model): name = models.CharField(max_length=127) category = models.ForeignKey('Category', on_delete=models.CASCADE, related_name='products') class Category(models.Model): name = models.CharField(max_length=127) parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
I want to get all categories that have at least one product-related or their children to have at least one product.
So I have a query like:
Category.objects.all().annotate(product_count_parent=Count('products')).filter(Q(product_count_parent__gt=0) | Q(children__id__in=Category.objects.filter(children=None).annotate(product_count=Count('products')).filter(product_count__gt=0)))
I have gotten this error
ProgrammingError: subquery must return only one column LINE 1: ... GROUP BY "nakhll_market_category"."id", T3."id", (SELECT U0...
Note:
See TracTickets
for help on using tickets.
You are using an
__in=QuerySet
filter without limiting the columns returned by your inner query.Please TicketClosingReasons/UseSupportChannels for further support.