如何在 asyncpg 中使用几何数据类型作为参数?
How to use geometry data type as a parameter in asyncpg?
对于上下文,我想查询一些大意为:
select point '(1,1)' <@ box '((0,0),(2,2))';
我试过这样的事情:
CREATE TABLE tbl (latlng POINT NOT NULL);
box = [[22.268764039073968, -140.09765625000003], [61.438767493682825, -56.42578125000001]]
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ ;', box)
然后我试了这个:
from asyncpg.types import Point, Box
v = [[22.268764039073968,-140.09765625000003],[61.438767493682825,-56.42578125000001]]
box = Box(Point(v[0][0], v[0][1]), Point(v[1][0], v[1][1]))
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ ;', box)
两次尝试都导致错误:
asyncpg.exceptions.AmbiguousFunctionError: operator is not unique: point <@ unknown
HINT: Could not choose a best candidate operator. You might need to add explicit type casts.
我猜我必须手动让 asyncpg 使用 box_encode() 作为这个参数(因为任何 table 定义都没有暗示它),但是我如何告诉 asyncpg 这样做呢?
(重复上面 Elvis 评论中给出的答案,以便我可以将问题标记为已解决)
解决办法是在cast的arg后面加上::box
,像这样:
box = [[22.268764039073968, -140.09765625000003], [61.438767493682825, -56.42578125000001]]
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ ::box;', box)
对于上下文,我想查询一些大意为:
select point '(1,1)' <@ box '((0,0),(2,2))';
我试过这样的事情:
CREATE TABLE tbl (latlng POINT NOT NULL);
box = [[22.268764039073968, -140.09765625000003], [61.438767493682825, -56.42578125000001]]
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ ;', box)
然后我试了这个:
from asyncpg.types import Point, Box
v = [[22.268764039073968,-140.09765625000003],[61.438767493682825,-56.42578125000001]]
box = Box(Point(v[0][0], v[0][1]), Point(v[1][0], v[1][1]))
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ ;', box)
两次尝试都导致错误:
asyncpg.exceptions.AmbiguousFunctionError: operator is not unique: point <@ unknown
HINT: Could not choose a best candidate operator. You might need to add explicit type casts.
我猜我必须手动让 asyncpg 使用 box_encode() 作为这个参数(因为任何 table 定义都没有暗示它),但是我如何告诉 asyncpg 这样做呢?
(重复上面 Elvis 评论中给出的答案,以便我可以将问题标记为已解决)
解决办法是在cast的arg后面加上::box
,像这样:
box = [[22.268764039073968, -140.09765625000003], [61.438767493682825, -56.42578125000001]]
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ ::box;', box)