Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuerySet.count(): IndexError: list index out of range #1590

Closed
PieceOfGood opened this issue Apr 23, 2024 · 3 comments · Fixed by #1593
Closed

QuerySet.count(): IndexError: list index out of range #1590

PieceOfGood opened this issue Apr 23, 2024 · 3 comments · Fixed by #1593
Labels
bug Something isn't working

Comments

@PieceOfGood
Copy link

Describe the bug

To Reproduce

class BaseModel(Model):
    class Meta:
        abstract = True

    id = fields.IntField(pk=True, index=True)


class BaseProp(BaseModel):
    class Meta:
        abstract = True

    name: str = fields.CharField(50, null=False, unique=True)


class Property(BaseProp):
    class Meta:
        table = "properties"

    customs: fields.ManyToManyRelation["Custom"]


class Custom(BaseModel):
    class Meta:
        table = "customs"

    short = fields.CharField(50, null=False, unique=True)

    properties: fields.ManyToManyRelation["Property"] = fields.ManyToManyField(
        "models.Property", related_name="customs"
    )


async def make() -> None:
    custom_one = await Custom.create(short="one")
    custom_two = await Custom.create(short="two")
    custom_three = await Custom.create(short="three")
    custom_four = await Custom.create(short="four")

    fine = await Property.create(name="fine")
    light = await Property.create(name="light")
    pink = await Property.create(name="pink")

    await custom_one.properties.add(fine, light, pink)
    await fine.customs.add(custom_two, custom_three, custom_four)
    await custom_three.properties.add(light)
    await custom_four.properties.add(pink, light)

async def main() -> None:
    await Tortoise.init(config=TORTOISE_CONFIG)
    await Tortoise.generate_schemas()

    await make()
    query = (
        Custom.filter(properties__id__in=[1, 2, 3])
        .annotate(count=Count("id"))
        .filter(count=3)
        .prefetch_related("properties")
    )

    result = await query
    print(result)
    for r in result:
        print(r.short, list(r.properties))

    print(await query.count())


if __name__ == '__main__':
    run_async(main())

Output:

[<Custom: 1>, <Custom: 4>]
one [<Property: 1>, <Property: 2>, <Property: 3>]
four [<Property: 1>, <Property: 3>, <Property: 2>]
Traceback (most recent call last):
  File "G:\pyProj\TortoiseModels\m2m_query_trouble.py", line 116, in <module>
    run_async(main())
  File "C:\Python\Python311\Lib\site-packages\tortoise\__init__.py", line 688, in run_async
    loop.run_until_complete(coro)
  File "C:\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "G:\pyProj\TortoiseModels\m2m_query_trouble.py", line 112, in main
    print(await query.count())
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Python311\Lib\site-packages\tortoise\queryset.py", line 1280, in _execute
    count = list(dict(result[0]).values())[0] - self.offset
                      ~~~~~~^^^
IndexError: list index out of range

Expected behavior

[<Custom: 1>, <Custom: 4>]
one [<Property: 1>, <Property: 2>, <Property: 3>]
four [<Property: 1>, <Property: 3>, <Property: 2>]
2

Additional context
Win 10
Python 3.11.5

@abondar
Copy link
Member

abondar commented Apr 23, 2024

Yeah, can confirm there is bug when propagating annotations info for non-filter queries, resulting in incorrect query generating

We'll look into fixing it

@abondar abondar added the bug Something isn't working label Apr 23, 2024
abondar added a commit that referenced this issue Apr 24, 2024
* Fix annotation propagation for non-filter queries (#1590)

* Fix lint

* Fix test
@abondar
Copy link
Member

abondar commented Apr 24, 2024

Will close when release will be ready

@abondar abondar reopened this Apr 24, 2024
@abondar
Copy link
Member

abondar commented May 13, 2024

Fixed in 0.20.1
Although there is related problem in #1607
Will be fixed in 0.21.0

@abondar abondar closed this as completed May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants