python - 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.
你能为这个问题提供一些帮助吗?
非常感谢!
解决方案
这需要您注册一个 UUID 类型:
import psycopg2.extras
psycopg2.extras.register_uuid()
使用上面的代码,您应该能够简单地使用:
arr = ArrayField(UUIDField, index=False)
推荐阅读
- reactjs - React Admin,基于第一个值的第二个输入过滤器
- android - 单击汉堡包图标时不会在 android studio 的片段中打开抽屉布局
- python - 如何在 Pandas 中将行拆分为由管道分隔的列
- java - 关于java中的日期格式
- r - 如何在左括号前插入空格
- laravel - 如何通过 OAuth 登录,以便 cron 作业可以运行 Laravel
- python - 从数据框中读取信息
- .net - C# 类库“使程序集 COM 可见”灰色或不可用
- ios - 通过 MethodChannel 从本机 iOS 端收到消息后,Flutter 活动未导航
- firebase - Firestore 中的最大数组大小是多少?