UUIDS 的 peewee ArrayField

peewee ArrayField of UUIDS

我正在尝试使用 peewee 操作一个 table,其中有一列包含一个 uuid 数组。

模型如下:

class test_db(BaseModel):
    arr = playhouse.postgres_ext.ArrayField(
        peewee.UUIDField,
        index_type=False
    )

我想在此 table 中插入一个条目,我正在使用以下代码:

arr = [uuid.UUID('d167169e-a017-4c17-8f3a-1dee98c1e563')]

x = test_db(arr=arr)
x.save()

但是我收到了

peewee.ProgrammingError: can't adapt type 'UUID'

这个我也试过了

arr = ['d167169e-a017-4c17-8f3a-1dee98c1e563']

x = test_db(arr=arr)
x.save()

但我收到以下错误:

peewee.ProgrammingError: column "arr" is of type uuid[] but expression is of type text[]
LINE 1: INSERT INTO "test_db" ("arr") VALUES (ARRAY['d167169e-a017-4...
                                              ^
HINT:  You will need to rewrite or cast the expression.

你能为这个问题提供一些帮助吗?

非常感谢!

这需要您注册一个 UUID 类型:

import psycopg2.extras
psycopg2.extras.register_uuid()

通过上面的代码,您应该可以简单地使用:

arr = ArrayField(UUIDField, index=False)