执行查询时出错 [英] Error while executing query
本文介绍了执行查询时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在此查询中收到一条错误消息
I get an error message on this query
query = "select count(*) from pgns_game where raw_moves = %s"
params = ('a',)
total_rows = self.model.objects.raw(query, params)
它说
InvalidQuery('Raw query must include the primary key')
我显然错过了一些东西,但我不知道是什么.有什么想法吗?
I am clearly missing something but I don't know what. Any ideas?
推荐答案
self.model.objects.raw()
期望查询结果包含来自模型 self.model的主键
,因此可以将它们转换为函数结果的对象列表.
self.model.objects.raw()
expects the query result to contain primary keys from the model self.model
, so it can turn these into a list of objects for the function result.
您真正需要做的是直接执行SQL,而不是通过管理器执行.您的代码可能看起来像这样:
What you really need to do is execute the SQL directly, and not through a manager. Your code will probably look like this:
from django.db import connection
cursor = connection.cursor()
cursor.execute("select count(*) from pgns_game where raw_moves = %s", ['a'])
total_rows = cursor.fetchone()
不过,我自己还没有尝试过.
I haven't tried this myself, though.
这篇关于执行查询时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文