Repository navigation
How to set check constraint in sql model? #292
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Apr 1, 2022 You can use various sa_column* args like:
Field(sa_column_args=[CheckConstraint('col1>5')])Field(sa_column=Column(...))
Reacted by Toni Alatalo, Ben Falk and Robin ElvinI use
sa_columnfor Enum like below:access_status: EnumAccessStatus = Field( sa_column=Column( SaEnumAccessStatus, nullable=False, ) )
One caveat to using columns is that you might need to move properties like
indexornullableinto the Column description. I found that it otherwise doesn't get honoured.E.g. the example in the above comment will work, but the below won't have the intended effect:
access_status: EnumAccessStatus = Field( sa_column=Column(SaEnumAccessStatus,), nullable=False, index=True)Reacted by Jonas, Toni Alatalo and zhaowu-anorHow about
CheckConstraintat the table level? I've been setting it in table_args like I'd do in sqlalchemy and just noticed Alembic isn't recognizing it.Reacted by AdrienSluicebox, Marko Stevanović, Ramtin Rassoli, Abrahan Munro, Matti Bragge, Juan Fernando, Matthew Meinzer, kbrowder and sh-at-csOne caveat to using columns is that you might need to move properties like
indexornullableinto the Column description. I found that it otherwise doesn't get honoured.
E.g. the example in the above comment will work, but the below won't have the intended effect:
access_status: EnumAccessStatus = Field( sa_column=Column(SaEnumAccessStatus,), nullable=False, index=True)In current version, you will get a runtime error if you try using
nullableorindexwithsa_column:RuntimeError: Passing nullable is not supported when also passing a sa_columnConsidering this comment from Sebastian
So I have intentionally simplified and limited the things I support in SQLModel, to work for the majority of use cases (say 90%) in a very simple and intuitive way (at least I hope so) without all the more advanced/exotic features that SQLAlchemy supports.
I think that
sa_columnis the only way to go..I know that that's pretty tangential to the question at hand, but, has anyone else encountered a problem where by setting
sa_columnafter you've had a field defined withoutsa_column, when making a migration with alembic, sqlalchemy will raise an exception saying something like:sqlalchemy.exc.ArgumentError: Column object 'amount' already assigned to Table 'balance'I've encountered this error while setting CheckConstraint, so, it's sort of related to the question posted here, but not really 😅
UPD: This is related to the inheritance. I suppose table is set on Column and consequently - Field on definition, so, if you have a base model (like I had for balance), you'll have to duplicate the field in your child models 😢
- added a commit that references this issue
on Mar 6, 2026 - locked and limited conversation to collaborators
on May 18, 2026
First Check
Commit to Help
Example Code
passDescription
SQLAlchemyallows to implementCheckContrainton either column or table level. How can I archive the same inSQLModel, e.g. limiting the possible values of a column to the colorsredandgreenor to ensure that the values within a column are greater than10.I suppose it must be somehow possible using
sa_column_kwargs(similar to implementingUniqueConstraint).Help would be highly appreciated, thanks!
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.8.1
Additional Context
No response