SQLAlchemy ORM从子查询中选择多个实体

时间:2023-01-28 23:46:26

I need to query multiple entities, something like session.query(Entity1, Entity2), only from a subquery rather than directly from the tables. The docs have something about selecting one entity from a subquery but I can't find how to select more than one, either in the docs or by experimentation.

我需要查询多个实体,例如session.query(Entity1,Entity2),只能从子查询而不是直接从表中查询。文档有关于从子查询中选择一个实体的内容,但我无法在文档中或通过实验找到如何选择多个实体。

My use case is that I need to filter the tables underlying the mapped classes by a window function, which in PostgreSQL can only be done in a subquery or CTE.

我的用例是我需要通过窗口函数过滤映射类底层的表,这在PostgreSQL中只能在子查询或CTE中完成。

EDIT: The subquery spans a JOIN of both tables so I can't just do aliased(Entity1, subquery).

编辑:子查询跨越两个表的JOIN,所以我不能只做别名(Entity1,子查询)。

2 个解决方案

#1


14  

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    bs = relationship("B")

class B(Base):
    __tablename__ = "b"

    id = Column(Integer, primary_key=True)

    a_id = Column(Integer, ForeignKey('a.id'))

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add_all([A(bs=[B(), B()]), A(bs=[B()])])
s.commit()

# with_labels() here is to disambiguate A.id and B.id.
# without it, you'd see a warning
# "Column 'id' on table being replaced by another column with the same key."
subq = s.query(A, B).join(A.bs).with_labels().subquery()


# method 1 - select_from()
print s.query(A, B).select_from(subq).all()

# method 2 - alias them both.  "subq" renders
# once because FROM objects render based on object
# identity.
a_alias = aliased(A, subq)
b_alias = aliased(B, subq)
print s.query(a_alias, b_alias).all()

#2


2  

I was trying to do something like the original question: join a filtered table with another filtered table using an outer join. I was struggling because it's not at all obvious how to:

我试图做一些类似于原始问题的事情:使用外部联接将过滤的表与另一个过滤的表联系起来。我很挣扎,因为它根本不明显如何:

  • create a SQLAlchemy query that returns entities from both tables. @zzzeek's answer showed me how to do that: get_session().query(A, B).
  • 创建一个SQLAlchemy查询,从两个表中返回实体。 @ zzzeek的回答告诉我如何做到这一点:get_session()。query(A,B)。

  • use a query as a table in such a query. @zzzeek's answer showed me how to do that too: filtered_a = aliased(A).
  • 在这样的查询中使用查询作为表。 @ zzzeek的回答告诉我如何做到这一点:filtered_a =别名(A)。

  • use an OUTER join between the two entities. Using select_from() after outerjoin() destroys the join condition between the tables, resulting in a cross join. From @zzzeek answer I guessed that if a is aliased(), then you can include a in the query() and also .outerjoin(a), and it won't be joined a second time, and that appears to work.
  • 在两个实体之间使用OUTER连接。在outerjoin()之后使用select_from()会破坏表之间的连接条件,从而产生交叉连接。从@zzzeek回答我猜想如果a是别名(),那么你可以在查询()和.outerjoin(a)中包含一个,它不会再次加入,这似乎有效。

Following either of @zzzeek's suggested approaches directly resulted in a cross join (combinatorial explosion), because one of my models uses inheritance, and SQLAlchemy added the parent tables outside the inner SELECT without any conditions! I think this is a bug in SQLAlchemy. The approach that I adopted in the end was:

遵循@ zzzeek建议的方法直接导致交叉连接(组合爆炸),因为我的一个模型使用继承,而SQLAlchemy在没有任何条件的情况下在内部SELECT之外添加了父表!我认为这是SQLAlchemy中的一个错误。我最终采用的方法是:

filtered_a = aliased(A, A.query().filter(...)).subquery("filtered_a")
filtered_b = aliased(B, B.query().filter(...)).subquery("filtered_b")
query = get_session().query(filtered_a, filtered_b)
query = query.outerjoin(filtered_b, filtered_a.relation_to_b)
query = query.order_by(filtered_a.some_column)

for a, b in query:
    ...

#1


14  

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    bs = relationship("B")

class B(Base):
    __tablename__ = "b"

    id = Column(Integer, primary_key=True)

    a_id = Column(Integer, ForeignKey('a.id'))

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add_all([A(bs=[B(), B()]), A(bs=[B()])])
s.commit()

# with_labels() here is to disambiguate A.id and B.id.
# without it, you'd see a warning
# "Column 'id' on table being replaced by another column with the same key."
subq = s.query(A, B).join(A.bs).with_labels().subquery()


# method 1 - select_from()
print s.query(A, B).select_from(subq).all()

# method 2 - alias them both.  "subq" renders
# once because FROM objects render based on object
# identity.
a_alias = aliased(A, subq)
b_alias = aliased(B, subq)
print s.query(a_alias, b_alias).all()

#2


2  

I was trying to do something like the original question: join a filtered table with another filtered table using an outer join. I was struggling because it's not at all obvious how to:

我试图做一些类似于原始问题的事情:使用外部联接将过滤的表与另一个过滤的表联系起来。我很挣扎,因为它根本不明显如何:

  • create a SQLAlchemy query that returns entities from both tables. @zzzeek's answer showed me how to do that: get_session().query(A, B).
  • 创建一个SQLAlchemy查询,从两个表中返回实体。 @ zzzeek的回答告诉我如何做到这一点:get_session()。query(A,B)。

  • use a query as a table in such a query. @zzzeek's answer showed me how to do that too: filtered_a = aliased(A).
  • 在这样的查询中使用查询作为表。 @ zzzeek的回答告诉我如何做到这一点:filtered_a =别名(A)。

  • use an OUTER join between the two entities. Using select_from() after outerjoin() destroys the join condition between the tables, resulting in a cross join. From @zzzeek answer I guessed that if a is aliased(), then you can include a in the query() and also .outerjoin(a), and it won't be joined a second time, and that appears to work.
  • 在两个实体之间使用OUTER连接。在outerjoin()之后使用select_from()会破坏表之间的连接条件,从而产生交叉连接。从@zzzeek回答我猜想如果a是别名(),那么你可以在查询()和.outerjoin(a)中包含一个,它不会再次加入,这似乎有效。

Following either of @zzzeek's suggested approaches directly resulted in a cross join (combinatorial explosion), because one of my models uses inheritance, and SQLAlchemy added the parent tables outside the inner SELECT without any conditions! I think this is a bug in SQLAlchemy. The approach that I adopted in the end was:

遵循@ zzzeek建议的方法直接导致交叉连接(组合爆炸),因为我的一个模型使用继承,而SQLAlchemy在没有任何条件的情况下在内部SELECT之外添加了父表!我认为这是SQLAlchemy中的一个错误。我最终采用的方法是:

filtered_a = aliased(A, A.query().filter(...)).subquery("filtered_a")
filtered_b = aliased(B, B.query().filter(...)).subquery("filtered_b")
query = get_session().query(filtered_a, filtered_b)
query = query.outerjoin(filtered_b, filtered_a.relation_to_b)
query = query.order_by(filtered_a.some_column)

for a, b in query:
    ...