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

Problem with Sum with F and Field validator MinValue #1502

Closed
zdets opened this issue Oct 20, 2023 · 0 comments · Fixed by #1750
Closed

Problem with Sum with F and Field validator MinValue #1502

zdets opened this issue Oct 20, 2023 · 0 comments · Fixed by #1750

Comments

@zdets
Copy link

zdets commented Oct 20, 2023

The Model

class Deal(models.Model):
   ...
    broker_payed_money = fields.FloatField(default=0, validators=[MinValueValidator(0)])

    broker_commission = fields.FloatField(null=False, validators=[GreaterThanValidator(0), LessThanValidator(1.0)])

    developer_payed_money = fields.FloatField(null=False, default=0, validators=[MinValueValidator(0)])
   ...

and the query

result = await Deal.filter(user=user).annotate(
        balance=Sum(F('developer_payed_money') * F('broker_commission') - F('broker_payed_money')),
    ).values_list('balance')

raises the Exception

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 161, in run_endpoint_function
    return await dependant.call(**values)
  File "/opt/project/app/routers/cabinet/users.py", line 25, in init_data
    return await init_data_handler(user)
  File "/opt/project/app/handlers/cabinet/users.py", line 31, in init_data_handler
    await _get_balances(user)
  File "/opt/project/app/handlers/cabinet/users.py", line 23, in _get_balances
    result = await Deal.annotate(
  File "/usr/local/lib/python3.10/site-packages/tortoise/queryset.py", line 1506, in _execute
    lst_values = list(map(listmap, result))
  File "/usr/local/lib/python3.10/site-packages/tortoise/queryset.py", line 1505, in <lambda>
    listmap = lambda entry: tuple(func(entry[column]) for column, func in columns)  # noqa
  File "/usr/local/lib/python3.10/site-packages/tortoise/queryset.py", line 1505, in <genexpr>
    listmap = lambda entry: tuple(func(entry[column]) for column, func in columns)  # noqa
  File "/usr/local/lib/python3.10/site-packages/tortoise/fields/base.py", line 225, in to_python_value
    self.validate(value)
  File "/usr/local/lib/python3.10/site-packages/tortoise/fields/base.py", line 244, in validate
    raise ValidationError(f"{self.model_field_name}: {exc}")
tortoise.exceptions.ValidationError: broker_payed_money: Value should be greater or equal to 0

But drop validator MinValueValidator from broker_payed_money avoids this problem.

Query column balance may be negative.

Version 0.19.2, tried 0.20.0

henadzit added a commit to henadzit/tortoise-orm that referenced this issue Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant