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 中的一个错误?有任何想法吗?