1

Django 1.5
PostgreSQL 9.2
psycopg2 2.4.6

我正在使用 QuerySet API 中的额外功能,以便能够为 Postgres 使用多维数据集扩展中的函数 - 我知道出于可移植性原因,额外功能不是很好,但无论如何我不会使用另一个数据库(不是在 Postgres 之后,不!)。所以问题是我从这段代码中得到了错误的 SQL 查询:

return self.select_related('item_place').extra(
            select={ 'distance': 'round(earth_distance(ll_to_earth(%s, %s), ll_to_earth(%s.latitude, %s.longitude))::numeric, 0)' },
            select_params=[latitude, longitude, ItemPlace._meta.db_table, ItemPlace._meta.db_table],
            where=['round(earth_distance(ll_to_earth(%s, %s), ll_to_earth(%s.latitude, %s.longitude))::numeric, 0) <= %s'],
            params=[latitude, longitude, ItemPlace._meta.db_table, ItemPlace._meta.db_table, radius])

似乎 psycopg2 用单引号将表名括起来,这对于 Postgres 是不正确的,在正在执行的脚本中我可以看到:

round(earth_distance(ll_to_earth(%s, %s), ll_to_earth('item_place'.latitude, 'item_place'.longitude))::numeric, 0)

我应该使用表名,因为我在另一个表中有纬度和经度,没有它我会得到“模糊列”错误。现在我不知道,也许我做错了,这就是为什么我得到这个错误或者它可能是 psycopg2 中的一个错误?有任何想法吗?

4

1 回答 1

2

根据docparamsandselect_params用于指示 Psycopg2 引用参数。它不适用于引用表名(通过双引号完成)。

引用Psycopg2 的文档

只有变量值应该通过这种方法绑定:它不应该用于设置表或字段名。对于这些元素,应该在运行 execute() 之前使用普通的字符串格式。

此外,我们通常不会使用需要双引号作为表名的标识符,请参阅此答案的注释。因此,直接在代码中使用表名是安全的:

return self.select_related('item_place').extra(
        select={ 'distance': 'round(earth_distance(ll_to_earth(%s, %s), ll_to_earth({tbl}.latitude, {tbl}.longitude))::numeric, 0)'.format(tbl=ItemPlace._meta.db_table) },
        select_params=[latitude, longitude],
        where=['round(earth_distance(ll_to_earth(%s, %s), ll_to_earth({tbl}.latitude, {tbl}.longitude))::numeric, 0) <= %s'.format(tbl=ItemPlace._meta.db_table)],
        params=[latitude, longitude])
于 2013-03-28T15:42:12.190 回答