明确指定" FROM"表

时间:2016-07-06 22:28:56

标签: python-3.x sqlalchemy

我目前有以下查询:

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"中删除studentschool表格。言。

1 个答案:

答案 0 :(得分:2)

aliased的{​​{1}}参数使用匿名别名,这意味着您传递给.join的{​​{1}}和Student是表格的不同“实例”。

School

在这里,您可以看到session.query在加入关系时可以使用哪个别名。