sql - Django 似乎错误地声称存在 SQL 语法错误

标签 sql django sqlite django-models

Django 似乎错误地声称我的 SQL 语法有错误。该查询在 django dbshel​​l 中运行良好(返回预期结果),但在通过 Django 运行查询时会产生错误。这是代码(回溯如下):

#this code is inside the models.Customer.display_sharers() function
sharers_by_action_count = Sharer.objects.raw('''
SELECT wordout_sharer.id, COUNT(actions_of_type.id) AS action_count
FROM
wordout_customer
INNER JOIN wordout_sharer
 ON wordout_sharer.customer_id = wordout_customer.id
LEFT JOIN wordout_click
 ON wordout_sharer.id = wordout_click.sharer_id
LEFT JOIN
(SELECT wordout_action.id, wordout_action.click_id
FROM wordout_action
WHERE
wordout_action.action_type_id = %s) as actions_of_type
 ON actions_of_type.click_id = wordout_click.id
WHERE wordout_customer.id = %s
GROUP BY wordout_sharer.id
ORDER BY action_count %s
''', (action_type_id, self.id, direction))

force_execution = list(sharers_by_action_count) #force the query to run by converting it to a list.. this is to trigger the error.

这是回溯:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/me/sources/django_wordout/../django_wordout/wordout/models.py", line 106, in display_sharers
    if order_by == 'action_count': #we have to make a special query for when they want to sort by the count of a specific action
  File "/Users/me/sources/django_wordout/../django_wordout/wordout/models.py", line 92, in sharers_by_action_count_with_total_clicks
    force_exec = list(sharers_by_action_count)
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1324, in __iter__
    query = iter(self.query)
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 67, in __iter__
    self._execute_query()
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 81, in _execute_query
    self.cursor.execute(self.sql, self.params)
  File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: near "?": syntax error

执行Database.Cursor.execute(self, query, params)时,以下是所有参数的值:

self :

<django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x10c477180> 

查询:

        SELECT wordout_sharer.id, COUNT(actions_of_type.id) AS action_count
        FROM
        wordout_customer
        INNER JOIN wordout_sharer
         ON wordout_sharer.customer_id = wordout_customer.id
        LEFT JOIN wordout_click
         ON wordout_sharer.id = wordout_click.sharer_id
        LEFT JOIN
        (SELECT wordout_action.id, wordout_action.click_id
        FROM wordout_action
        WHERE
        wordout_action.action_type_id = ?) as actions_of_type
         ON actions_of_type.click_id = wordout_click.id
        WHERE wordout_customer.id = ?
        GROUP BY wordout_sharer.id
        ORDER BY action_count ?

参数:

         (1, 1, 'DESC')

我在 Django 中发现了错误吗?它是否无法正确处理某些类型的查询?

我的配置:我正在运行一个非常普通的开发配置。对于数据库引擎,我使用 sqlite。对于迁移(这是最新的),我使用的是 South。

更新

刚刚发现将 %s 替换为它们的值可以使查询正常工作。可能发生的情况会导致 %s 对 Django 造成问题?

最佳答案

According to this page ,sqlite 将参数化值视为文字,这意味着它们插入给定类型的常量,而不是进行常规的“文本替换”。

由于 order by 中的 ASC 或 DESC 是关键字,而不是常量,因此不能用参数替换。

这实际上会带来一些可能意想不到的行为,例如,如果您这样做

ORDER BY ? DESC

并且您给它一个列名称(例如column1),它实际上会运行,但不会按您预期的顺序排序。原因是它实际上按字符串“column1”排序 - 每行都相同 - 而不是实际的列内容。

关于sql - Django 似乎错误地声称存在 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8951506/

相关文章:

javascript - 仅更新 Put Restify + SQLite3 上的某些字段

android - arraylist 没有正确更新

php - 使用 PHP 从 MySQL 选择并回显单个字段

css - Django 只应用我的 css 文件中的第一个样式

django - 用Docker Compose设置Django/PostgreSQL项目的正确方法是什么?

用于分面的 Python 继承或额外属性

sql - 如何在 SQLite3 和 Rails 3.1 中打开 REGEXP?

mysql - MySQL 中何时使用单引号、双引号和反引号

sql - 按天而不是时间对时间戳进行分组

c# - 在 C# 中存储查询还是在 Postgres 中使用存储函数?