tortoise / tortoise/tortoise-orm

IntField: constraints not taken into account

Open
#1,853 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
The IntField has the property constraints. It contains the constraints ge and le. These constraints are not taken into account (in any place as far as I can tell) or they should be reflected by a validator.

To Reproduce

from tortoise import models, fields, run_async
from tortoise.contrib.test import init_memory_sqlite


class Acc(models.Model):
    id = fields.IntField(pk=True)
    some = fields.IntField()


async def main():
    constraints_id = Acc._meta.fields_map.get('id').constraints  # {'ge': -2147483648, 'le': 2147483647}
    too_high_id = constraints_id.get('le') + 1
    constraints_some = Acc._meta.fields_map.get('some').constraints  # {'ge': -2147483648, 'le': 2147483647}
    too_low_some = constraints_some.get('ge') - 1
    acc = Acc(id=too_high_id, some=too_low_some)  # this should throw an error
    await acc.save()  # or maybe this


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

Expected behavior
Either the constraints should match the capabilities of the DB or the constraints should be checked beforehand. And an error should be thrown.

Additional context
The produced Pydantic Model by pydantic_model_creator would check those constraints.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with IntField.constraints and the Acc.save() path shown in the reproduction, using init_memory_sqlite to reproduce values outside the ge/le range. Compare this behavior with the constraints enforced by pydantic_model_creator; done means the constraints are either aligned with database capabilities or invalid values reliably raise an error before persistence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.