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

Why do two logically similar queries in web2py give different results?

我已经为此苦苦挣扎了一段时间,只是随心所欲地尝试改变条件。为什么它以一种方式起作用而不是另一种方式?

关于这个 table 定义:

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'

两个查询都不正确,因为您使用了 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'