2013-07-15 79 views
0

我試圖讓搜索繼續,我通過一個生成器將幾個不同的音節傳入'and_'函數。然後,我的搜索應返回所有並且只有那些具有所有給定音節的單詞sqlalchemy在and_ filters中使用生成器

matches = db.session.query(Word).join(Word.syllables).filter(db.and_(Syllable.syllable ==對於組合中的syl)))all()

當前不返回任何內容。這種方法甚至有意義嗎?

回答

0

我不認爲這個查詢會做你想做的。你會生成查詢會是這樣的組合=(1,2):

SELECT words.* FROM words INNER JOIN syllables ON words.id = syllables.word_id WHERE syllables.syllable = 1 AND syllables.syllable = 2 

我想你想的是這對組合=(1,2):

SELECT words.* FROM words INNER JOIN syllables AS s1 ON words.id = s1.word_id AND s1.syllable = 1 INNER JOIN syllables AS s2 ON words.id = s2.word_id AND s2.syllable = 2 

重複加入可能會很慢,但這裏是你如何能在SQLAlchemy中做到這一點:

query = db.session.query(Word) 
for syl in combo: 
    syl_cls = aliased(Syllable) 
    query = query.join(syl_cls, Word.syllables).filter(syl_cls.syllable == syl) 
matches = query.all() 

如果你生成或條款則此查詢將是一個簡單得多。

相關問題