[英]Django Error “django.db.utils.ProgrammingError: subquery has too many columns ”
The raw query itself is correct and I am able to get retrieve the rawqueryset from the db.原始查询本身是正确的,我能够从数据库中检索原始查询集。 I need to convert this into queryset for further processing and I am facing below error.我需要将其转换为查询集以进行进一步处理,并且我面临以下错误。
Creating corresponding django query was hard for me and that is why I created SQL query, got the raw query set and now attempting to convert it to query set for further processing.创建相应的 django 查询对我来说很难,这就是为什么我创建 SQL 查询,获取原始查询集,现在尝试将其转换为查询集以进行进一步处理。
I have changed django model names and table names for anonymity.为了匿名,我更改了 django model 名称和表名。
Here is the output of what I tried in django shell.这是我在 django shell 中尝试的 output。 I was able to execute the below query but getting the error "django.db.utils.ProgrammingError: subquery has too many columns" when I try to access "queryset" below.我能够执行下面的查询,但是当我尝试访问下面的“queryset”时收到错误“django.db.utils.ProgrammingError: subquery has too many columns”。
from django.db.models.expressions import RawSQL
from xyz.models import *
value = '1.2.3.4'
queryset = Test1.objects.filter(id__in=RawSQL("SELECT DISTINCT ON (test1.start_time, test1.id) test1.id, test1.name, test1.start_time FROM test1 WHERE EXISTS (SELECT * FROM test2 JOIN test3 ON test2.test3_id = test3.id AND test3.value = %s JOIN test4 ON test2.test4_id = test4.id AND test4.test1_id = test1.id) ORDER BY test1.start_time DESC", params=[value]))
For readability I have formatted the query used below.为了便于阅读,我已经格式化了下面使用的查询。
SELECT
DISTINCT ON (test1.start_time, test1.id)
test1.id,
test1.name,
test1.start_time
FROM
test1
WHERE
EXISTS (
SELECT
*
FROM
test2
JOIN test3 ON test2.test3_id = test3.id
AND test3.value = 'value'
JOIN test4 ON test2.test4_id = test4.id
AND test4.test1_id = test1.id
)
ORDER BY
test1.start_time DESC
As the error states, you are selecting too many columns which your filter condition does not require.如错误所示,您选择的列太多,您的过滤条件不需要。 You subquery must select only Test1
table id
field as you filter on id
field.在过滤id
字段时,您的子查询必须 select 仅Test1
表id
字段。 So basically you subquery should only project id
field like this:所以基本上你的子查询应该只像这样投影id
字段:
SELECT
test1.id
FROM
test1
WHERE
EXISTS (
SELECT
*
FROM
test2
JOIN test3 ON test2.test3_id = test3.id
AND test3.value = 'value'
JOIN test4 ON test2.test4_id = test4.id
AND test4.test1_id = test1.id
)
ORDER BY
test1.start_time DESC
So your final queryset should be like:所以你的最终查询集应该是这样的:
queryset = Test1.objects.filter(id__in=RawSQL("SELECT test1.id FROM test1 WHERE EXISTS (SELECT * FROM test2 JOIN test3 ON test2.test3_id = test3.id AND test3.value = %s JOIN test4 ON test2.test4_id = test4.id AND test4.test1_id = test1.id) ORDER BY test1.start_time DESC", params=[value]))
You don't need the RawSQL at all你根本不需要 RawSQL
from django.db.models import Exists, OuterRef
queryset = Test1.objects.filter(
Exists(Test2.objects.filter(
test3__value=value,
test4__test1_id=OuterRef('id')
)
)
I'm not certain your django model names, so test3
, test4
, and test1
may be a little different in your example.我不确定您的 django model 名称,因此test3
、 test4
和test1
在您的示例中可能会有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.