简体   繁体   English

SQLAlchemy ORM 动态连接不返回所有连接表的所有列

[英]SQLAlchemy ORM dynamic join not returning all columns from all joined tables

I am new to sqlalchemy. So any help is appreciated.我是 sqlalchemy 的新手。非常感谢您的帮助。 I have a function that constructs my queries for my application.我有一个 function 可以为我的应用程序构造查询。 I pass it a list of tables to join.我将要加入的表列表传递给它。

Here are the relevant code snippets.这是相关的代码片段。

class Scope(Base):
    entry = Column(String(512))
    location_id = Column(Integer, ForeignKey('location_id'))
    type = Column(String(128))

class Location(Base):
    id = Column(Integer, primary_key=True)
    name = Column(String(512)
    modified_by = Column(String(128))

instances = [Scope, Location]
join_classes = [Location]

queryset = session.query(*instances).join(*join_classes).all()

Here is the SQL query that runs (when I print queryset to screen before the.all()):这是运行的 SQL 查询(当我在 .all() 之前将查询集打印到屏幕时):

queryset: SELECT scope.id AS scope_id, scope.location_id AS scope_location_id, scope.entry AS scope_entry, scope.type AS scope_type, location.name AS location_name, location.modified_by AS location_modified_by,查询集:SELECT scope.id AS scope_id,scope.location_id AS scope_location_id,scope.entry AS scope_entry,scope.type AS scope_type,location.name AS location_name,location.modified_by AS location_modified_by,
FROM scope JOIN location ON location.id = scope.location_id FROM scope JOIN location ON location.id = scope.location_id

My end result I want is: a list of dictionaries for all columns (from both tables - like regular inner join gives a single table).我想要的最终结果是:所有列的字典列表(来自两个表 - 就像常规的内部连接给出一个表)。

However, I am getting the a list when I type(queryset) and when I just try to do [u._asdict() for u in queryset] which is how I return a list of dictionaries in queries that don't have a join, it only returns a list of dictionaries for 1 column from each table (the column in the __repr__.但是,当我键入(queryset)时以及我只是尝试在查询集中为您执行[u._asdict()]时,我得到了一个列表,这就是我在没有连接的查询中返回字典列表的方式,它只返回每个表中 1 列的字典列表(__repr__.

I need all columns from both tables to be returned.我需要返回两个表中的所有列。

Right now this is how what is what I get: [{'Scope': 192.168.0.0/24, 'Location': main}, ...]现在这就是我得到的: [{'Scope': 192.168.0.0/24, 'Location': main}, ...]

I need something like, where all columns from the join are returned in a list of dictionaries: [{'Scope.entry': 192.168.0.0/24, 'Scope.type': 'virtual', 'Location.name': main, 'Location.modified_by': 'jim'}, ...]我需要类似的东西,其中连接的所有列都在字典列表中返回: [{'Scope.entry': 192.168.0.0/24, 'Scope.type': 'virtual', 'Location.name': main , 'Location.modified_by': 'jim'}, ...]

In my code the instances & join_classes are dynamically passed and not hard coded as different functions pass the table models to join on (with the 1st model table being the table that all proceeding join on).在我的代码中,实例和 join_classes 是动态传递的,而不是硬编码的,因为不同的函数传递要加入的表模型(第一个 model 表是所有后续加入的表)。 I need this to work with a join on multiple tables (but all tables will be joined to the 1st model table, Scope in this example.)我需要这个来连接多个表(但所有表都将连接到第一个 model 表,在此示例中为 Scope。)

Edit : I finally realized I was getting a list of sqlalchemy table objects back.编辑:我终于意识到我得到了 sqlalchemy 表对象的列表。 That is why I was getting the __repr__ values when displaying.这就是为什么我在显示时得到 __repr__ 值的原因。

Well, writing something done definitely helps you figure out the answer. 好吧,写点已完成的事情绝对可以帮助您找到答案。

For anyone that might benefit this is what I did. 对于任何可能受益的人,这就是我所做的。 I am sure there is a more eloquent way to do this so please let me know if so. 我敢肯定有一种更雄辩的方法可以做到这一点,所以请让我知道。

I finally read my output correctly and realized that it was giving me 2 table model objects (1 per table joined). 我终于正确地读取了输出,并意识到它给了我2个表模型对象(每个连接的表1个)。 I then iterated over each, converted each iteration to a list of dictionaries and then merged those dictionaries appropriately so that i had one list of dictionaries like a inner join table would give me. 然后,我遍历每个字典,将每次迭代转换为字典列表,然后适当地合并这些字典,这样我就有一个字典列表,例如内部联接表会给我。

Here is some of my code: 这是我的一些代码:

for obj in queryset:
    result.append(queryset_to_dict(obj))

for r in result:
    new_dict = {}
    for inner in r:
        new_dict = {**new_dict, **inner}
    new_result.append(new_dict)
  • Note the queryset_to_dict is a function I created for converting sqlalchemy table model objects to list of dictionaries. 注意queryset_to_dict是我创建的用于将sqlalchemy表模型对象转换为字典列表的函数。
records = DBSession.query(GeneralLedger, ConsolidatedLedger).join(ConsolidatedLedger, GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId).all()

this sort of query return both table data这种查询返回两个表数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM