我目前有以下查询:
query = self.session.query(Student, School).join(
Person.student, aliased=True).join(
Student.school, aliased=True).filter(
Person.id == 1)
编译到这个SQL。
SELECT student.id AS student_id, student.school_id AS student_school_id, student.person_id AS student_person_id, school.id AS school_id, school.name AS school_name
FROM student, school, person JOIN student AS student_1 ON person.id = student_1.person_id JOIN school AS school_1 ON school_1.id = student_1.school_id
WHERE person.id = :id_1
我希望查询保持原样,但我希望from语句完全来自Person
模型。像
SELECT * FROM person JOIN ... WHERE person.id = :id_1
我认为aliased
kwarg搞砸了条件。删除aliased
kwarg会修复行为,但我需要使用别名kwarg来处理特殊用例。如何从" FROM"中删除student
和school
表格。言。
答案 0 :(得分:2)
aliased
的{{1}}参数使用匿名别名,这意味着您传递给.join
的{{1}}和Student
是表格的不同“实例”。
School
在这里,您可以看到session.query
在加入关系时可以使用哪个别名。