ge and le for Field should create CheckConstraint
#1722
Replies: 1 comment
-
|
If you want the database to enforce it too, add the check with from sqlalchemy import CheckConstraint
from sqlmodel import SQLModel, Field
class UserWithSQLModel(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
age: int = Field(ge=18, le=100, nullable=False)
__table_args__ = (
CheckConstraint("age >= 18 AND age <= 100", name="ck_age_range"),
)Now you get both layers: Auto-generating the constraint from |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
For
Integerfield I already set limits usinggeandle. It would be nice to have it as a constraint also.Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.31
Python Version
3.14
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions