为什么.count()在带有|的peewee查询中返回错误的数字(联盟)?

时间:2014-06-06 20:23:14

标签: python sqlite peewee

下面的前三个查询返回正确的数字,而最后一个查询返回错误的数字。它应该返回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

1 个答案:

答案 0 :(得分:1)

请尝试使用.wrapped_count()