Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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在PostgresJSONB中查找嵌套值?_Python_Postgresql_Sqlalchemy - Fatal编程技术网

Python 如何使用sqlalchemy在PostgresJSONB中查找嵌套值?

Python 如何使用sqlalchemy在PostgresJSONB中查找嵌套值?,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我有一个带有JSONB列名的postgres表entity。json结构的示例如下: { "some_key": "some value", "properties": { "name": ["name_value"], } } 我需要通过name\u value查找记录。我可以使用查询来获得它: SELECT entity FROM my_table where entit

我有一个带有JSONB列名的postgres表
entity
。json结构的示例如下:

{
  "some_key": "some value",
  "properties": {
    "name": ["name_value"],
  }
}
我需要通过
name\u value
查找记录。我可以使用查询来获得它:

SELECT entity FROM my_table where entity->'properties' @> {"name": ["name_value"]};
问题是:我找不到使用sqlalchemy ORM创建相同查询的方法

更新: 我的模型是动态的,因为多个表使用相同的结构,只是表名不同。代码如下:

。。。
Base=声明性_Base(引擎)
类ForeignBase(AbstractConcreteBase,Base):
通过
def make_型号(名称):
类实体(ForeignBase):
__tablename\uu=名称
__mapper_args_uu={“多态性_标识”:名称,“具体”:真}
__表_args_uu={“扩展_现有”:True}
id=列(字符串,主键=True)
实体=列(JSONB,nullable=True)
...
配置映射器()
返回实体
然后,我使用3个函数启动我的模型,并获得我当前需要的一个:

def get_model_list():
tables\u names\u list=get\u table\u name\u list()
模型列表=[为表名称列表中的表名称创建模型(前缀+表名称)]
返回模型列表
def get_tables_dict():
返回{table.\uuuu tablename\uuuuu:ForeignBase中表的表。\uuuu子类\uuuuuu()}
def get_table_对象(表名称):
返回get\u tables\u dict().get(table\u name)

您可以使用以下SQLAlchemy表达式。运算符@>是JSONB列的SQLAlchemy中的contains()

Session().query(MyTable).filter(MyTable.entity[“properties”]”。包含({“name”:[“name\u value”]}))。带有_个实体(MyTable.entity)。all()

谢谢您的回答。不幸的是,它返回一个空列表
[]
。它还返回相同的结果,不带
和\u entities
part。打印并检查它生成的查询。可能某些筛选器丢失我已尝试打印该语句并从my_表中选择my_table.entity,其中((my_table.entity->%(entity_1)s))@>%(param_1)s。看起来应该没问题。只是不确定那些双括号。查询看起来没问题。双括号不是问题。可能数据不在数据库中?数据在数据库中,我将原始select放入数据库shell,它会找到记录。我仔细检查过了。其他查询中也会出现相同的记录。它与
MyTable
模型配置有什么关系?