为什么web2py中两个逻辑上相似的查询会给出不同的结果?

时间:2016-02-02 16:35:21

标签: python web2py data-access-layer

我一直在努力解决这个问题,并试着随心所欲地改变周围的条件。为什么它以单向而不是另一种方式工作?

在此表定义上:

db.define_table('bids',
                 Field('body', 'text', label="Application"),
                 Field('selected', 'string', requires=IS_IN_SET(['Yes', 'No']), readable=False, writable=False, default='No', widget=SQLFORM.widgets.radio.widget, label="Select this application"), 
                 Field('confirmed', 'string', requires=IS_IN_SET(['Yes', 'No']), readable=False, writable=False, default='No', widget=SQLFORM.widgets.radio.widget, label="Confirm acceptance"),
                 Field('delivered', 'string', requires=IS_IN_SET(['Yes', 'No']), readable=False, writable=False, default='No'),
                 Field('posted_on', 'datetime', readable=True, writable=False),
                 Field('posted_by', 'reference auth_user', readable=False, writable=False),
                 Field('job_id', 'reference jobs', readable=False, writable=False)
                 )

此查询生成正确的数据

query = db.bids.job_id == job_id and db.bids.delivered=='No' and db.bids.selected =='Yes' and db.bids.confirmed=='Yes'

虽然这个没有

query = db.bids.job_id == job_id and db.bids.selected =='Yes' and db.bids.confirmed=='Yes' and db.bids.delivered=='No'

1 个答案:

答案 0 :(得分:2)

这两个查询都不正确,因为您使用了and而不是&(并且无法将每个条件包装在括号中)。它应该是:

((db.bids.job_id == job_id) & (db.bids.delivered == 'No') &
 (db.bids.selected == 'Yes') & (db.bids.confirmed == 'Yes'))

原始查询:

db.bids.job_id == job_id and db.bids.delivered=='No' and db.bids.selected =='Yes' and db.bids.confirmed=='Yes'

简单等同于:

True and True and True and db.bids.confirmed == 'Yes'

导致最终查询中只有一个条件:

db.bids.confirmed == 'Yes'