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

Do not validate when loading from the db #1750

Merged

Conversation

henadzit
Copy link
Contributor

@henadzit henadzit commented Oct 27, 2024

Description

Motivation and Context

None of the ORMs that I checked validate records when loading them from the database:

I do not see a reason why validating the record on loading should be a default behavior. I can see that you might want to do it in some cases but you can always call .validate explicitly. But I can imagine the issue where you add a new validator, run a data migration to fix the data but while the migration is being run, the reads are failing.

I also ran a benchmark where I was loading 100 testmodels.ValidatorModel records from the database and on average not running validation is ~5% faster for a model with many validators. I do not think at all that this is a decisive argument but something to keep in mind.

How Has This Been Tested?

from tortoise import Tortoise, fields, run_async
from tortoise.functions import Sum
from tortoise.models import Model
from tortoise.expressions import F, Q
from tortoise.validators import MinValueValidator

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

    broker_commission = fields.FloatField(null=False)

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


async def main():
    # Initializing Tortoise and creating the schema

    await Tortoise.init(  # type: ignore
        db_url="sqlite://:memory:",
        modules={"models": ["__main__"]},
    )
    await Tortoise.generate_schemas()

    await Deal.create(
        test=1, broker_payed_money=10, broker_commission=0.1, developer_payed_money=20
    )

    result = await Deal.annotate(
        balance=Sum(F("developer_payed_money") * F("broker_commission") - F("broker_payed_money")),
    ).values_list("balance")
    print("res", result)


run_async(main())

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@henadzit henadzit mentioned this pull request Oct 27, 2024
7 tasks
@coveralls
Copy link

Pull Request Test Coverage Report for Build 11540154248

Details

  • 5 of 6 (83.33%) changed or added relevant lines in 3 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 89.008%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/backends/sqlite/executor.py 3 4 75.0%
Files with Coverage Reduction New Missed Lines %
tortoise/backends/sqlite/executor.py 1 77.14%
Totals Coverage Status
Change from base Build 11529966516: -0.02%
Covered Lines: 5974
Relevant Lines: 6598

💛 - Coveralls

@henadzit
Copy link
Contributor Author

henadzit commented Nov 1, 2024

@abondar, would love to hear your feedback on this one!

@abondar abondar merged commit 0c05405 into tortoise:develop Nov 1, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem with Sum with F and Field validator MinValue
3 participants