SQLAlchemy: filter many-to-many joinedload -
i have current table setup many many association between "words" , "phrases":
association_table_wp = table('words_phras', base.metadata, autoload=true, extend_existing=true) class phrase(base): __tablename__ = 'phrases' __table_args__ = {'autoload': true} def __init__(self, phrase, explanation=none, active=false): self.phrase = phrase self.explanation = explanation self.active = active class word(base): __tablename__ = 'words' __table_args__ = {'autoload': true} def __init__(self, word, word_id=none, active=false): self.word = word self.word_id = word_id self.active = active word.phrases = relationship(phrase, secondary=association_table_wp, backref="words") now when want words , phrases use:
words = db_session.query(word).filter(word.id.in_(word_ids)).\ options(joinedload(word.phrases)).\ all() this allows me access words[i].phrases access phrases associated given word.
now, works fine, note "active" property. want filter words' phrases ones active == true returned. how can done? have tried several combinations of joins , eager_loading, none of them have worked i'd hope.
thank much.
i think contains_eager() you're looking for:
words = db_session.query(word).\ join(word.phrases).\ options(contains_eager(word.phrases)).\ filter(word.id.in_(word_ids)).\ filter(phrase.active == true)
Comments
Post a Comment