下面的前三个查询返回正确的数字,而最后一个返回错误的数字。它应该返回 153,而不是返回 8193。我不知道这个数字是从哪里来的。
遍历查询正确返回 153 条记录。
>>> Project.select().where(Project.number.between('2012-01', '2012-02')).count()
75
>>> Project.select().where(Project.number.between('2012-02', '2012-03')).count()
78
>>> Project.select().where(Project.number.between('2012-01', '2012-03')).count()
153
>>> (Project.select().where(Project.number.between('2012-01', '2012-02')) |
Project.select().where(Project.number.between('2012-01', '2012-03'))).count()
8193
编辑
这是一个从空数据库开始重现问题的函数。
def test(self):
db = peewee.SqliteDatabase('test.db', check_same_thread=False)
class Test(peewee.Model):
num = peewee.IntegerField()
class Meta:
database = db
Test.drop_table(True)
Test.create_table(True)
for i in range(1, 11):
Test.create(num=i)
q = Test.select().where(Test.num > 6) | Test.select().where(Test.num > 7)
print(q)
print('Count =', q.count())
for i in q:
print(i.num)
这是它的输出。它表明迭代正确返回 4 个项目,但计数错误:
<class 'DocFinder.DocFinder.DocFinder.test.<locals>.Test'> SELECT t2."id", t2."num" FROM "test" AS t2 WHERE (t2."num" > ?) UNION SELECT t3."id", t3."num" FROM "test" AS t3 WHERE (t3."num" > ?) [6, 7]
Count = 7
7
8
9
10
最佳答案
尝试使用 .wrapped_count()
代替。
关于python - 为什么 .count() 在带有 | 的 peewee 查询中返回错误的数字(联盟)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24090072/