如何在asyncpg中使用几何数据类型作为参数?

问题描述 投票:0回答:1

对于上下文,我想查询一些具有以下效果的东西:

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 <@ $1;', 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 <@ $1;', 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()作为此参数(因为任何表定义都没有暗示它,但是我如何告诉asyncpg做到这一点?

python postgresql bounding-box asyncpg
1个回答
0
投票

(重复上面猫王评论中给出的答案,所以我可以将问题标记为已解决)

解决方案是在arg之后添加::box,如下所示:

box = [[22.268764039073968, -140.09765625000003], [61.438767493682825, -56.42578125000001]]
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ $1::box;', box)
© www.soinside.com 2019 - 2024. All rights reserved.