【问题标题】:How to insert an element into a nested JSONB array with SQLAlchemy on postgres如何在 postgres 中使用 SQLAlchemy 将元素插入到嵌套的 JSON 数组中
【发布时间】:2020-01-10 17:06:54
【问题描述】:

我已经看到了很多关于如何在 postgres 中存储 JSONB 的好答案,但是从 Postgres 9.5 开始,我们现在能够插入现有的 JSONB 数组而不更新列中的数据。我能找到的所有材料都没有记录在任何地方,而且由于我是 SQLAlchemy 的新手(以及一些 Python),所以阅读代码并没有像我想要的那样真正帮助我。

我正在使用 Postgres 10.9、python 3.7 和 SQLAlchemy 1.3.8(带有 GINO 包装器)

这只是最新的尝试,但有很多不同的错误:

await gathering. \
        update(participation=func.jsonb_insert("participation",
                                                "{applications}",
                                                func.to_jsonb(json.dumps(application)))). \
        apply()

participation 列中,我有一个 JSONB 对象

{ 
    "applications": [ { ... element to be appended} ] 
}

在这种特殊情况下,代码会产生错误:

无法确定多态类型,因为输入的类型未知

【问题讨论】:

    标签: python postgresql jsonb gino


    【解决方案1】:

    想通了..

    ajs = json.dumps(application)
    await gathering. \
                    update(participation=func.jsonb_insert(
                           json.dumps(gathering.participation), 
                           ["applications","0"],  ajs)). \
                    apply()
    

    这里是反复试验。 GOT 有比这更优雅的解决方案。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2016-07-06
      • 2021-04-17
      • 1970-01-01
      • 2023-03-21
      • 2015-04-02
      • 1970-01-01
      • 2021-03-07
      • 2017-04-26
      相关资源
      最近更新 更多