Skip to content

Commit

Permalink
fix save with F expression and field with source_field (#630)
Browse files Browse the repository at this point in the history
* fix save with F expression and field with source_field

* fix style for executor.py file

* add some tests for "fix save with F expression and field with source_field"
  • Loading branch information
vaal- authored Feb 4, 2021
1 parent a25c0fc commit 5a66aa6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
17 changes: 16 additions & 1 deletion tests/test_source_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is to test that behaviour doesn't change when one defined source_field parameters.
"""
from tests.testmodels import SourceFields, StraightFields
from tests.testmodels import NumberSourceField, SourceFields, StraightFields
from tortoise.contrib import test
from tortoise.expressions import F
from tortoise.functions import Coalesce, Count, Length, Lower, Trim, Upper
Expand Down Expand Up @@ -259,3 +259,18 @@ async def test_values_by_fk(self):
class SourceFieldTests(StraightFieldTests):
def setUp(self) -> None:
self.model = SourceFields # type: ignore


class NumberSourceFieldTests(test.TestCase):
def setUp(self) -> None:
self.model = NumberSourceField

async def test_f_expression_save(self):
obj1 = await self.model.create()
obj1.number = F("number") + 1
await obj1.save()

async def test_f_expression_save_update_fields(self):
obj1 = await self.model.create()
obj1.number = F("number") + 1
await obj1.save(update_fields=["number"])
4 changes: 4 additions & 0 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,7 @@ class ValidatorModel(Model):
comma_separated_integer_list = fields.CharField(
max_length=100, null=True, validators=[CommaSeparatedIntegerListValidator()]
)


class NumberSourceField(Model):
number = fields.IntField(source_field="counter", default=0)
8 changes: 6 additions & 2 deletions tortoise/backends/base/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pypika.terms import ArithmeticExpression, Function

from tortoise.exceptions import OperationalError
from tortoise.expressions import F
from tortoise.fields.base import Field
from tortoise.fields.relational import (
BackwardFKRelation,
Expand Down Expand Up @@ -267,11 +268,14 @@ def get_update_sql(
db_column = self.model._meta.fields_db_projection[field]
field_object = self.model._meta.fields_map[field]
if not field_object.pk:
if db_column not in arithmetic_or_function.keys():
if field not in arithmetic_or_function.keys():
query = query.set(db_column, self.parameter(count))
count += 1
else:
query = query.set(db_column, arithmetic_or_function.get(db_column))
value = F.resolver_arithmetic_expression(
self.model, arithmetic_or_function.get(field)
)[0]
query = query.set(db_column, value)

query = query.where(table[self.model._meta.db_pk_column] == self.parameter(count))

Expand Down

0 comments on commit 5a66aa6

Please sign in to comment.