gpt4 book ai didi

Flask SqlAlchemy : Returning None for JSON type inserts 'null' string, 不是真正的 NULL

转载 作者:行者123 更新时间:2023-12-05 02:30:40 25 4
gpt4 key购买 nike

我正在使用 Flask SqlAlchemy。我正在将数据插入 Postgres 数据库,并且有一个 JSON 列类型。当没有数据时,我返回 None,目的是将 NULL 设置到列中。这是建议 here .

if someCondition:
json_obj = json.dumps(my_dict)
return json_obj
else:
return None # this inserts a 'null' string incorrectly
# Only this works correctly:
return null() # imported from sqlalchemy,

但我看到这会插入一个 "null" 字符串。获得真正的 NULL 的唯一方法是使用 sqlalchemy.null()。其他人在这个 thread 中注意到了同样的事情.以下是两种方法的区别。有人可以确认 None 不应该用于 NULL 插入,以消除混淆吗?

enter image description here

最佳答案

您描述的是默认行为。

JSONJSONB 列类型可以通过将参数 none_as_null 设置为 True 来初始化。此参数默认为 False

documentation供引用。

关于Flask SqlAlchemy : Returning None for JSON type inserts 'null' string, 不是真正的 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71785363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com