[英]How do I use PostgreSQL's jsonb_to_record in SQLAlchemy?
PostgreSQL has functions like jsonb_to_record
or jsonb_populate_record
which allow you convert data stored in a JSONB column to separate columns. PostgreSQL 具有jsonb_to_record
或jsonb_populate_record
等函数,它们允许您将存储在 JSONB 列中的数据转换为单独的列。 however, I'm using SQLAlchemy and jsonb_to_record
requires a predefined structure for the output (see example below from the PostgreSQL docs)但是,我使用的是 SQLAlchemy 并且jsonb_to_record
需要一个预定义的输出结构(请参阅下面来自 PostgreSQL 文档的示例)
select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}') as x(a int, b text, d text)
Is there a way to use these functions from SQLAlchemy?有没有办法从 SQLAlchemy 使用这些函数?
you can use any postgres function in SQLAlchemy with func like so:您可以使用 func 在 SQLAlchemy 中使用任何 postgres 函数,如下所示:
from SQLAlchemy import func
session.query(func.json_to_record(Model.jsonb_field))
Do note that set-returning functions are a bit of a pain to deal with with SQLAlchemy, but if you want you can get it to work.请注意,使用 SQLAlchemy 处理返回集合的函数有点麻烦,但如果您愿意,可以让它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.