首页 > 解决方案 > UUIDS 的 peewee ArrayField

问题描述

我正在尝试使用 peewee 操作一个表,该表有一列包含一组 uuid。

模型如下:

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

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

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.

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

非常感谢!

标签: pythonpostgresqluuidpeewee

解决方案


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

import psycopg2.extras
psycopg2.extras.register_uuid()

使用上面的代码,您应该能够简单地使用:

arr = ArrayField(UUIDField, index=False)

推荐阅读