tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

IntField: constraints not taken into account

Open markus-96 opened this issue 1 year ago • 0 comments

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.

markus-96 avatar Jan 17 '25 10:01 markus-96