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

标签 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

最佳答案

尝试使用 .wrapped_count() 代替。

关于python - 为什么 .count() 在带有 | 的 peewee 查询中返回错误的数字(联盟)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24090072/

相关文章:

python - 如何重新循环程序回到开头

列表中的 Python 3 奇数函数行为

perl - SQLite 的磁盘 I/O 错误

Assets 文件夹中的Android外部数据库

python-3.x - 是否可以在 EFS 中可靠地使用 SQLite?

python - 如何在列标题中实现 QLineEdit 以过滤 QTreeView 内的数据?

python - 属性错误: 'list' object has no attribute '_meta'

python - 用 Python 编写数独求解器

python - 我用 Python 编写的广义 Student-T 概率分布没有积分为 1(在某些情况下)

python - 我如何知道如何正确使用方法来访问某些变量/数据集?