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 这样做呢?

4

1 回答 1

0

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

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

box = [[22.268764039073968, -140.09765625000003], [61.438767493682825, -56.42578125000001]]
await pool.fetch(f'SELECT * FROM tbl WHERE latlng <@ $1::box;', box)
于 2020-05-30T05:54:19.983 回答