Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在SQLAlchemy中查询由JSON列过滤的数据?_Python_Postgresql_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Python 如何在SQLAlchemy中查询由JSON列过滤的数据?

Python 如何在SQLAlchemy中查询由JSON列过滤的数据?,python,postgresql,sqlalchemy,flask-sqlalchemy,Python,Postgresql,Sqlalchemy,Flask Sqlalchemy,我正在用Flask和Flask SQLAlchemy编写一个应用程序 我有一个models.py文件,如下所示 来自sqlalchemy.dialogs.postgresql导入JSON 自定义类(db.Model): __tablename_uu='custom' data=db.Column(JSON) 数据字段的值如下所示: [ {"type": "a string", "value": "value stri

我正在用Flask和Flask SQLAlchemy编写一个应用程序

我有一个
models.py
文件,如下所示

来自sqlalchemy.dialogs.postgresql导入JSON
自定义类(db.Model):
__tablename_uu='custom'
data=db.Column(JSON)
数据
字段的值如下所示:

[
    {"type": "a string", "value": "value string"},
    {"type": "another", "value": "val"},
    ...
]

现在,我想查询所有
Custom
对象,这些对象的
数据
字段在其对象列表中包含如下对象
{“type”:“anything”,“value”:“what I want”}

假设表名为“Custom”,json字段名为“data”下面的sql语句将获得值子字段等于“what I want”的结果

根据,可以使用
cast

从sqlalchemy.types导入Unicode
Custom.query.filter(Custom.data['value'].astext.cast(Unicode)=“我想要什么”)

可在此处找到相关文档:
sql = text("select * from custom where data->>'value'= 'what I want'")
result = db.engine.execute(sql)