[Django] #33775: subquery must return only one column

717 views
Skip to first unread message

Django

unread,
Jun 9, 2022, 8:48:28 AM6/9/22
to django-...@googlegroups.com
#33775: subquery must return only one column
-------------------------------------+-------------------------------------
Reporter: Mohammad | Owner: nobody
Ali Mehdizadeh |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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...
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33775>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 9, 2022, 10:41:46 AM6/9/22
to django-...@googlegroups.com
#33775: subquery must return only one column
-------------------------------------+-------------------------------------
Reporter: Mohammad Ali | Owner: nobody
Mehdizadeh |
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: invalid
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 Simon Charette):

* status: new => closed
* resolution: => invalid


Comment:

You are using an `__in=QuerySet` filter
[https://docs.djangoproject.com/en/4.0/ref/models/querysets/ without
limiting the columns returned by your inner query].

Please TicketClosingReasons/UseSupportChannels for further support.

--
Ticket URL: <https://code.djangoproject.com/ticket/33775#comment:1>

Reply all
Reply to author
Forward
0 new messages