You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
fromtypingimportAnnotated, LiteralfromsqlmodelimportField, SQLModelfrompydanticimportAfterValidatorROLES_SET= {"student", "teacher", "admin"}
defcheck_role(x: str):
assertxinROLES_SET, f"role must be in {'student', 'teacher', 'admin'}"returnxclassUserBase(SQLModel):
role: Annotated[str, AfterValidator(check_role)] # worked# role: Literal["student", "teacher", "admin"] # when use it with fastapi,the User class will trigger error.# role: Annotated[str, Field(regex=r'^(student|teacher|admin)$')] #no error,but not work,and string can passclassUser(UserBase, table=True):
id: int|None=Field(default=None, primary_key=True)
Description
In my code, I write two class extend from SQLmodel.
Attr 'role'(str) in class should in ["student", "teacher", "admin"].
In the normal python /pydantic class system, I can use role: Literal["student", "teacher", "admin"]。
But when I set it in a class extend SQLmodel, I get error:
File "/home/leo/myspace/collaborative_learning_system/server/models/tables.py", line 76, in
class User(UserBase, table=True):
File "/home/leo/myspace/collaborative_learning_system/server/venv/lib/python3.12/site-packages/sqlmodel/main.py", line 559, in new
col = get_column_from_field(v)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/leo/myspace/collaborative_learning_system/server/venv/lib/python3.12/site-packages/sqlmodel/main.py", line 708, in get_column_from_field
sa_type = get_sqlalchemy_type(field)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/leo/myspace/collaborative_learning_system/server/venv/lib/python3.12/site-packages/sqlmodel/main.py", line 656, in get_sqlalchemy_type
if issubclass(type_, Enum):
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class
As a substitute, I think maybe can use role: Annotated[str, Field(regex=r'^(student|teacher|admin)$')] ,but when I use it with fastapi, I found any string can be inserted into the database(postgresql).
In addition,I found regex in the Field from pydantic can work! If use it, the code like: role: Annotated[str, Field(partten=r'^(student|teacher|admin)$')]
Final, I have to use: Annotated[str, AfterValidator(check_role)], it can work ,but not simple and not grace.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
In my code, I write two class extend from SQLmodel.
Attr 'role'(str) in class should in ["student", "teacher", "admin"].
In the normal python /pydantic class system, I can use
role: Literal["student", "teacher", "admin"]
。But when I set it in a class extend SQLmodel, I get error:
File "/home/leo/myspace/collaborative_learning_system/server/models/tables.py", line 76, in
class User(UserBase, table=True):
File "/home/leo/myspace/collaborative_learning_system/server/venv/lib/python3.12/site-packages/sqlmodel/main.py", line 559, in new
col = get_column_from_field(v)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/leo/myspace/collaborative_learning_system/server/venv/lib/python3.12/site-packages/sqlmodel/main.py", line 708, in get_column_from_field
sa_type = get_sqlalchemy_type(field)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/leo/myspace/collaborative_learning_system/server/venv/lib/python3.12/site-packages/sqlmodel/main.py", line 656, in get_sqlalchemy_type
if issubclass(type_, Enum):
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class
As a substitute, I think maybe can use
role: Annotated[str, Field(regex=r'^(student|teacher|admin)$')]
,but when I use it with fastapi, I found any string can be inserted into the database(postgresql).In addition,I found regex in the Field from pydantic can work! If use it, the code like:
role: Annotated[str, Field(partten=r'^(student|teacher|admin)$')]
Final, I have to use:
Annotated[str, AfterValidator(check_role)]
, it can work ,but not simple and not grace.Operating System
Linux
Operating System Details
ubuntu23 (wsl)
SQLModel Version
0.0.22
Python Version
python3.12.3
Additional Context
Beta Was this translation helpful? Give feedback.
All reactions