如何在多一关系中相交的值SQLalchemy?

发布于 2025-02-13 09:57:17 字数 431 浏览 0 评论 0原文

我有表<代码>老师。它包含与表student的多一关系。学生包含唯一的列名称。我如何找到包含某些名字的学生的所有老师?
例如:
Tothert1包含学生1名称“鲍勃”和sut剂品2的学生。

thoston2包含带名称为“ alice”和sut剂的sut剂品的Student2和名称为“ mark”的Student3。
thoston3包含名称为“鲍勃”的Student1。
thoston4包含名称为“ mark”的Student3。
我得到名称[“ Alice”,“ Mark”]
在此示例中,我必须得到老师1、2、4。
如何编写此Sqlalchemy查询?
session.query(offer).filter(...)。所有()

I have table Teacher. It contains many-to-many relationship with table Student. Student contains unique column name. how can I find all the teachers that contain students with certain names?
For example:
Teacher1 contains Student1 with name "Bob" and Student2 with name "Alice".
Teacher2 contains Student2 with name "Alice" and Student3 with name "Mark".
Teacher3 contains Student1 with name "Bob".
Teacher4 contains Student3 with name "Mark".
I get names ["Alice", "Mark"].
In this example I have to get Teacher 1, 2, 4.
How to write this sqlalchemy query?
session.query(Teacher).filter(...).all()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-02-20 09:57:17

避免您的协会多一对多的模型/TABELE是studentTeacher,并且您正在寻找names =(“ Alice”,“ Mark”,),此查询应返回什么您期望:

results = session.query(
   Teacher
).join(StudentTeacher).join(Student).filter(
   Student.name.in_(names)
).all()

Asumming that your association many-to-many model/tabele is StudentTeacher and you looking for names=("Alice", "Mark",), this query should return what you expected:

results = session.query(
   Teacher
).join(StudentTeacher).join(Student).filter(
   Student.name.in_(names)
).all()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文