简体   繁体   English

使用 SQLAlchemy,我可以在查询中加入 Python object(即列表)吗?

[英]Using SQLAlchemy, can I join a Python object (i.e. a list) within a Query?

Assuming I have a Postgres-DB table looking like this (with with many more rows):假设我有一个看起来像这样的 Postgres-DB 表(有更多行):

|------|-------|
|col1  |col 2  |
|------|-------|
|a1    |10     |
|a2    |55     |
|a3    |24     |
|------|-------|

And a List in Python with Tuples looking like this: Python 中的列表如下所示:

|------|-------|
|a1    |1      |
|a3    |2      |
|------|-------|

During runtime I now want to (inner) join the table with the list without having to persist the list as a DB object.在运行时,我现在想(内部)将表与列表连接起来,而不必将列表作为 DB object 保存。

What does work, is to filter using the list and SQLAlchemys .in_ operator.起作用的是使用列表和 SQLAlchemys .in_运算符进行过滤。

However when I try db.query(Table).join(list) I get this error:但是,当我尝试db.query(Table).join(list)时,出现此错误:

sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type sqlalchemy.exc.NoInspectionAvailable:没有适用于 object 类型的检测系统

Of course a workaround would be to first fetch all elements from the db using the filter operator and then using python for the join...but it feels like there should be a当然,一种解决方法是首先使用过滤器运算符从数据库中获取所有元素,然后使用 python 进行连接......但感觉应该有一个

If I understood correctly and you want to only get the rows where col1 == tuple[0] , you can use the sqlalchemy filter and in_ operators:如果我理解正确并且您只想获取col1 == tuple[0]的行,则可以使用 sqlalchemy filterin_运算符:

tuples_list = [('a1', 1), ('a3', 2)]

search_dict = dict(tuples_list)  # Convert to dict 

res = db.query(Table).filter(Table.col1.in_(search_dict.keys())).all()

for instance in res:
    instance.col2 = instance.col2 + search_dict[instance.col1]

There's also a detailed example without using the ORM here: SQLAlchemy IN clause这里还有一个不使用 ORM 的详细示例: SQLAlchemy IN 子句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 中是否有一种简短的方法来检查 object 是否类似于列表(即“序列”但不是“字符串”)? - Is there a short way in Python to check if an object is list-like (i.e. a `Sequence` but not a `str`)? 一种在python中存储素数的方法(即,有一个列表检查元素是否存在,并且可以按顺序输出元素) - A way to store primes in python (i.e. have a list that checks if a element is there, and that can output elements in order) 在 Python 中获取唯一单词列表(即,不重复) - Getting a list of unique words (i.e., not duplicates) in Python 重新链接python对象(即通过引用传递) - Relink python object (i.e. pass by reference) Python如何使用枚举和列表跳过第一(即零)迭代? - Python how to skip the first (i.e. zero) iteration using enumerate and a list? 如果该子列表中的任何元素在另一个列表中,如何删除列表中的列表(即子列表)? - how to delete a list within a list (i.e., a sublist) if any element of that sublist is in another list? 我如何在 python 中使用 sqlalchemy 查询关系数据库 - how can i query a relational database using sqlalchemy in python 创建一个按类型对电影进行分组的边缘列表,即加入相同类型的两部电影 - create an edge list that groups films by genre, i.e. join two films of the same genre 我可以从共享文件中导入模块列表吗? 即我可以进口进口吗? - Can I import a list of modules from a shared file? i.e. can I import imports? 如何在SQLAlchemy(PostgreSQL / Python)中的右外部联接样式查询中更新一列? - How can I update one column in a right outer join style query in SQLAlchemy (PostgreSQL/Python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM