为什么此SQLAlchemy查询返回未请求的不合逻辑值?

时间:2012-08-15 21:07:10

标签: sql sqlalchemy

    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())

返回

[{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'}]

2 个答案:

答案 0 :(得分: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))
           )

答案 1 :(得分:0)

认为这是加入,看来你在PostOption上加入Post to PostOptions.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)
)