SQL Alchemy查询与热切加载的关系?

时间:2015-08-07 19:33:00

标签: python sqlalchemy

我在查询中使用SA的急切关系加载时遇到了问题。

我有两个表如此声明:

class Score(Base):
    __tablename__ = 'Score'

    school = relationship("School", backref='score')
    SchoolKey = Column('SchoolKey', Integer, primary_key=True)


class School(Base):
    __tablename__ = 'School'

    SchoolKey = deferred(Column('SchoolKey', Integer, primary_key=True))
    SchoolName = Column('SchoolName', String)
    SchoolDistrict = deferred(Column('SchoolDistrict', String), group = 'district')
    SchoolDistrictID = deferred(Column('SchoolDistrictID', integer), group = 'district')

我想尽可能简明地使用来自School表的过滤条件查询我的Score表。所以我的关系急切地加载了。

现在当我查询时我无法让它工作:

session.query(Score).filter(School.SchoolName == 'RandomName')

(这实际上只会让我的电脑崩溃)任何人都知道如何让它发挥作用?

背景:我知道这对我的例子来说听起来微不足道,但得分是一个事实表,位于> 10个表的核心。我真的想避免将10个.join部分写入任何要过滤的查询。所以我想避免这样的事情:

session.query(Score).join(School).filter(School.SchoolName == 'RandomName')

1 个答案:

答案 0 :(得分:0)

临时解决方案:接受我需要为所有维度表编写连接:

var judges = [];


    $(document).ready(function () {
        $.getJSON('/Home/GetJudges', function (result) {
            judges = result;
            $(".select2").select2({
                placeholder: "Search for a Judge",
                data: { results: result, id: "Id", text: "Text" }
            });
        });
    });