简体   繁体   English

为什么我从 Django 查询中收到此 sql 语法错误?

[英]Why am I getting this sql syntax error from a Django query?

In Django I have the following queries in a view在 Django 我有以下查询

applicant=Applicants.objects.get(ben=entity_number)
f471s=applicant.form471_set.order_by("-funding_year","number")
enrollment=f471s.values("schooldata__ben").annotate(f=Max("number")).filter(
           number=F("f")).values().aggregate(
           s=Sum("schooldata__student_count"))['s']

When I try to access the page for that view, I get the error当我尝试访问该视图的页面时,出现错误

DatabaseError
(1064, "You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use 
near 'FROM (SELECT `frontend_form471`.`number` AS `number`,
`frontend_form471`.`form_s' at line 1")

I had the logger print out the SQL that Django is generating and I got我让记录器打印出 Django 正在生成的 SQL,我得到了

(0.001) SELECT  FROM (SELECT `frontend_form471`.`number` AS `number`,
`frontend_form471`.`form_status` AS `form_status`, `frontend_form471`.`ben_id`
AS `ben_id`, `frontend_form471`.`funding_year` AS `funding_year`,
MAX(`frontend_form471`.`number`) AS `f` FROM `frontend_form471` LEFT OUTER JOIN
`SchoolData` ON (`frontend_form471`.`number` = `SchoolData`.`f471 Application Number`)
WHERE (`frontend_form471`.`ben_id` = 122871 ) GROUP BY `frontend_form471`.`number`,
`frontend_form471`.`number`, `frontend_form471`.`form_status`,
`frontend_form471`.`ben_id`, `frontend_form471`.`funding_year` HAVING
`frontend_form471`.`number` =  MAX(`frontend_form471`.`number`) ORDER BY
`frontend_form471`.`funding_year` DESC, `frontend_form471`.`number` ASC) 
subquery; args=(u'122871',)

For reference, I am using MySQL 5.1.54-1ubuntu4.作为参考,我使用的是 MySQL 5.1.54-1ubuntu4。 It looks like the syntax error is in the first line, but I do not understand why Django is generating SQL code with improper syntax.看起来语法错误在第一行,但我不明白为什么 Django 会生成语法不正确的 SQL 代码。 Is there any way I can change something (a setting, possibly) to fix this?有什么办法可以改变一些东西(可能是一个设置)来解决这个问题?

Edit: in response to an answer, I also tried running the third line of python code without the second .values() call and got exactly the same effect.编辑:作为对答案的回应,我还尝试在没有第二个.values()调用的情况下运行 python 代码的第三行并得到完全相同的效果。

In case other folks are wondering as I was, this is an accepted Django bug: https://code.djangoproject.com/ticket/15624如果其他人像我一样想知道,这是一个公认的 Django 错误: https://code.djangoproject.com/ticket/15624

I ended up avoiding the problem by replacing my third line of python code with:我最终通过将第三行 python 代码替换为:

latest=SchoolData.objects.filter(f471__ben=entity_number).values(
       'ben').annotate(m=Max('f471__number')).order_by().distinct()
enrollment=sum(SchoolData.objects.filter(ben=l['ben']).get(
           f471__number=l['m']).student_count for l in latest)

This worked but it was very slow.这行得通,但速度很慢。 In the end some extra information about my data allowed me to optimize like this:最后,一些关于我的数据的额外信息使我能够像这样进行优化:

latestYear=f471s.aggregate(m=Max('funding_year'))['m']
enrollment=SchoolData.objects.filter(f471__ben=entity_number,
           f471__funding_year=latestYear).distinct().aggregate(
           s=Sum('student_count'))['s']

which does not do exactly the same thing but solved my particular problem better.这并不完全相同,但更好地解决了我的特定问题。

The issue is the second empty values() clause after the annotate.问题是注释后的第二个空 values() 子句。 You're literally telling django not to include any columns in the select.您实际上是在告诉 django 不要在 select 中包含任何列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM