Replies: 5 comments 6 replies
-
I am facing a similar issue with Enums
where I get error like this
|
Beta Was this translation helpful? Give feedback.
-
I have the same error. (using Pydantic 2.7 and SQLModel 0.0.16. ) So I use Pydantic 1.0.11 and SQLModel 0.08. It works. If anyone has same problem, hope it helps. |
Beta Was this translation helpful? Give feedback.
-
Hello there, in fact this error is triggered by sqlalchemy. If you remove the from sqlalchemy import Uuid
class UserRoleLink(SQLModel, table=True):
__tablename__ = "user_role_links"
user_id: Optional[UUID4] = Field(default=None, foreign_key="user.id", primary_key=True, sa_type=Uuid)
role_id: Optional[UUID4] = Field(
default=None, foreign_key="roles.id", primary_key=True, sa_type=Uuid
) @hakkano @ChrisNi888 |
Beta Was this translation helpful? Give feedback.
-
For me, the issue was caused by using Model: class AccessControlDB(SQLModel, table=True):
__tablename__ = "access_control"
id: UUID = Field(default=uuid4, primary_key=True)
created_at: datetime | None= Field(default=None)
attribute: str
operation: typing.Literal['CREATE', 'UPDATE', 'DELETE', 'READ']
user_id: UUID The error is specifically caused by this line: Error: File "<redacted_path>\database\models\attribute.py", line 17, in <module>
class AccessControlDB(SQLModel, table=True):
File "<redacted_path>\site-packages\sqlmodel\main.py", line 482, in __new__
col = get_column_from_field(v)
^^^^^^^^^^^^^^^^^^^^^^^^
File "<redacted_path>\site-packages\sqlmodel\main.py", line 626, in get_column_from_field
sa_type = get_sqlalchemy_type(field)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<redacted_path>\site-packages\sqlmodel\main.py", line 575, in get_sqlalchemy_type
if issubclass(type_, Enum):
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class My current interpretation is that SQLModel is implicitly converting a typing.Literal into an Enum but it's not being handled correctly. I resolved this error by changing my implementation to using an actual enum.Enum Solution: class OperationEnum(Enum):
CREATE = 'CREATE'
UPDATE = 'UPDATE'
DELETE = 'DELETE'
READ = 'READ'
class AccessControlDB(SQLModel, table=True):
__tablename__ = "access_control"
id: UUID = Field(default=uuid4, primary_key=True)
created_at: datetime | None= Field(default=None)
attribute: str
operation: OperationEnum
user_id: UUID |
Beta Was this translation helpful? Give feedback.
-
Same here, I just posted this #981 I didn't see this thread when I was first searching |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
When a model has one or more Optional fields, SQLModel raises an error. If the Optionals are removed, it works fine. It happens for all the defined models using Optionals. Also if the code is changed to use the
Optional[]
format.It errors when starting the application and when running
pytest
.The traceback:
I have added a simple print in the
def get_sqlalchemy_type(field: Any) -> Any:
function, just before theissubclass
checks:It happens with SQLModel
0.0.14
and with the currentmaster
.Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.14
Python Version
Python 3.11.0
Additional Context
Looks similar to #57
Beta Was this translation helpful? Give feedback.
All reactions