2012-08-15 45 views
0
posts_query = (db.session.query(Post). 
        join(PostOption,PostOption.post_id == post_obj.id). 
        filter(and_(PostOption.key == "name", 
           PostOption.val == 'a')). 
        filter(Post.place_id == post_obj.place_id) 
        ) 

    print map(lambda x:get_post_values(x),posts_query.all()) 

回報爲什麼這個SQLAlchemy查詢返回不需要的邏輯值?

[{u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'a'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}, {u'name': u'b'}] 
+0

OT:請不要使用lambda x:get_post_values(x)其中get_post_values就足夠了。 – 2012-08-16 16:29:59

+0

哎呀,你是對的。深夜編碼及其危險。 – nubela 2012-08-16 19:19:00

回答

0

認爲它的加入,看來你要加入後到PostOptions PostOption.post_id =一些文字值。認爲你的意思是這樣的:

posts_query = (db.session.query(Post). 
    join(PostOption, PostOption.post_id == Post.id). 
    filter(and_(PostOption.key == "name", PostOption.val == 'a')). 
    filter(Post.place_id == post_obj.place_id). 
    filter(Post.id == post_obj.id) 
) 
+0

wow gr8 catch!完全錯過了,謝謝! – nubela 2012-08-16 19:19:18

0

胡亂猜測這裏:

附上and_內place_id條款:

posts_query = (db.session.query(Post). 
      join(PostOption,PostOption.post_id == post_obj.id). 
      filter(and_(PostOption.key == "name", 
         PostOption.val == 'a', 
         Post.place_id == post_obj.place_id)) 
      ) 
+0

試過了,不起作用:(另外,這個修復也不合邏輯,結果sql查詢也是一樣的, – nubela 2012-08-16 06:11:19

相關問題