How to set check constraint in sql model? #1951
Replies: 7 comments
-
|
You can use various sa_column* args like:
|
Beta Was this translation helpful? Give feedback.
-
|
I use access_status: EnumAccessStatus = Field(
sa_column=Column(
SaEnumAccessStatus,
nullable=False,
)
) |
Beta Was this translation helpful? Give feedback.
-
|
One caveat to using columns is that you might need to move properties like E.g. the example in the above comment will work, but the below won't have the intended effect:
|
Beta Was this translation helpful? Give feedback.
-
|
How about |
Beta Was this translation helpful? Give feedback.
-
In current version, you will get a runtime error if you try using Considering this comment from Sebastian
I think that |
Beta Was this translation helpful? Give feedback.
-
|
I know that that's pretty tangential to the question at hand, but, has anyone else encountered a problem where by setting
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 😢 |
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
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
Beta Was this translation helpful? Give feedback.
All reactions