Sqlalchemy:多对多连接字段,其中field = max和extra fields

时间:2016-12-02 01:08:23

标签: python sqlalchemy

我有一组看起来像这样的表:

Inputs =========  Parts ========= Outputs
        (n, m)     ||     (n, m) 
                   ||
                 (n, m)
                   ||
                   ||
               Productions

class Input(DeclarativeBase):
    __tablename__ = 'inputs'
    id = Column(Integer, primary_key=True, autoincrement=True)
    some_attr1 = Column(Unicode(length)
    some_attr2 = Column(Unicode(length)

class Output(DeclarativeBase):
    __tablename__  = 'outputs'
    id = Column(Integer, primary_key=True, autoincrement=True)
    some_attr1 = Column(Unicode(length)
    some_attr2 = Column(Unicode(length)
    extra_attr1 = Column(Unicode(length)
    parts = relationship('Part', backref='output', 
                         passive_deletes='all', 
                         passive_updates=True)

class Part(DeclarativeBase):
    __tablename__ = 'parts'
    id = Column(Integer, primary_key=True, autoincrement=True)
    id_input = Column(ForeignKey('inputs.id'))
    id_output = Column(ForeignKey('outputs.id'))
    extra_attr = Column(Unicode(length))

class Production(DeclarativeBase):
    __tablename__ = 'productions'
    id = Column(Integer, primary_key=True, autoincrement=True)
    date = Column(DateTime, default=get_current_time)
    flag = Column(Boolean)
    other_attr1 = Column(Unicode(length))
    parts = relationship('Parts',
                         secondary="productionsteps",
                         backref=backref("productions", lazy='dynamic'))

productionsteps = Table('productionsteps', 
                        BASE.metadata,
                        Column('id_production', 
                               Integer, 
                               ForeignKey('productions.id)),
                        Column('id_part', 
                               Integer, 
                               ForeignKey('parts.id')),
                        UniqueConstraint('id_production',
                                         'id_part',
                                         name='uix_productionsteps'))

我正在尝试查询每个输出子集的最新“产品”(生成与输出相关的一组部件),其中生产日期低于DATE1且生产标志等于BOOL1。

1 /获取早于DATE1的每个输出的最新产品

subquery1 = (
    session.query(Output.id.label("id_output"),
                  func.max(Production.date).label("max_date"))
           .join(Part, Production.parts)
           .join(Output, Part.output)
           .filter(and_(Output.some_attr1 == attr1,
                        Output.some_attr2 == attr2,
                        Production.date<=DATE1))
           .group_by(Output.id)
           .subquery()
)

好的,那个查询似乎有用......

2 /但是,我想将这些结果加入OutputProduction表的其他列(+ {1}}上的应用过滤器)。

我找不到怎么做!!!

sqlalchemy的新手,我将不胜感激任何建议或帮助。

1 个答案:

答案 0 :(得分:0)

解决了查询:

function updateHeader(newHeader, currentView)
    {
        t.options.header = newHeader;
        headerElement = header.render();

        updateHeaderTitle();
        updateTodayButton();
        header.activateButton(currentView);

        //replace current header with new header
        if (headerElement) {
            $("div.fc-toolbar").remove();
            element.prepend(headerElement);
        }
    }