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

add tests: update on filtered query fails when using subquery #1802

Open
Invisi opened this issue Dec 5, 2024 · 2 comments
Open

add tests: update on filtered query fails when using subquery #1802

Invisi opened this issue Dec 5, 2024 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Invisi
Copy link

Invisi commented Dec 5, 2024

The original bug was fixed but we obviously lack tests that would have prevented the issue. This issue can be resolved by adding more tests covering usage of Subquery with updates. This might be a good first time issue.

Original bug report

Describe the bug
As of #1777 / 7f077c1 and version 0.22.1, update queries using tortoise.expressions.Subquery in filters fail with the following exceptions:

# sqlite3
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied.
# postgres
tortoise.exceptions.OperationalError: invalid input for query argument $1: 'test' ('str' object cannot be interpreted as an integer)
# from postgres' log
UPDATE "event" SET "name"=$1 WHERE "tournament_id" IN (SELECT "id" "0" FROM "tournament" WHERE "id"=$1)

To Reproduce
Run the following sample code under tortoise-orm==0.22.1:

from tortoise.expressions import Subquery
from tortoise.models import Model
from tortoise import fields
from tortoise import Tortoise
import asyncio


class Tournament(Model):
    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=255)


class Event(Model):
    id = fields.IntField(primary_key=True)
    tournament = fields.ForeignKeyField("models.Tournament", related_name="events")
    name = fields.CharField(max_length=255)


async def init():
    await Tortoise.init(
        config={
            "connections": {"default": "sqlite://:memory:"},
            "apps": {
                "models": {
                    "models": ["__main__"],
                    "default_connection": "default",
                },
            },
            "use_tz": True,
        }
    )
    await Tortoise.generate_schemas()


async def main():
    await init()

    await Tournament.create(name="abc")
    await Event.create(tournament_id=1, name="abc")

    # this succeeds
    affected = await Event.filter(tournament_id=1).update(name="test")
    print("0", affected)

    # this succeeds too
    events = await Event.filter(
        tournament_id__in=Subquery(Tournament.filter(id=1).values_list("id", flat=True))
    )
    print("1", events)

    # this fails
    affected = await Event.filter(
        tournament_id__in=Subquery(
            Tournament.filter(id=1).values_list("id", flat=True)
        ),
    ).update(name="test")
    print("2", affected)

    await Tortoise.close_connections()


if __name__ == "__main__":
    asyncio.run(main())

Expected behavior
The generated query should be correct, rows matching the filter should be updated.

Additional context
The good news is that it's fixed in #1797, however I cannot say why or how that specific PR fixes the underlying issue.

@Abdeldjalil-H
Copy link
Contributor

A similar issue with filter #1800

@henadzit henadzit added the bug Something isn't working label Dec 5, 2024
@henadzit
Copy link
Contributor

henadzit commented Dec 5, 2024

@Invisi, thank you for a great bug report with a reproducible example! I just merged #1797 and we will release a new version soon. I would like to repurpose your issue as an opportunity to add more tests that would have prevented the regression. This could be a good first time issue.

@henadzit henadzit added the good first issue Good for newcomers label Dec 5, 2024
@henadzit henadzit changed the title regression: update on filtered query fails when using subquery add tests: update on filtered query fails when using subquery Dec 6, 2024
@henadzit henadzit added enhancement New feature or request and removed bug Something isn't working labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants