Open christianholland opened 2 years ago
You can use various sa_column* args like:
Field(sa_column_args=[CheckConstraint('col1>5')])
Field(sa_column=Column(...))
I use sa_column
for 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 index
or nullable
into 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)
How about CheckConstraint
at 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.
First Check
Commit to Help
Example Code
Description
SQLAlchemy
allows to implementCheckContraint
on either column or table level. How can I archive the same inSQLModel
, e.g. limiting the possible values of a column to the colorsred
andgreen
or 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