Replies: 1 comment
-
This is a duplicate of #735 ( I'll close this discussion. Let's track these issues there |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
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