sqlmodel
sqlmodel copied to clipboard
TypeError: issubclass() arg 1 must be a class when having a field with a complex type
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
from datetime import datetime
from typing import Optional, List, Tuple, Dict, Union
from sqlmodel import Field, SQLModel, create_engine
class SemanticSearch(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id_user: int
date_time: datetime
query: str
clean_query: str
semantic_search_result: List[Tuple[int, Dict[str, Union[str, int, float]]]]
## sqlite
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)
Description
I would like to create a semantic_search table in the database.db sqlite database with the 6 fields specified.
The last field semantic_search_result with List[Tuple[int, Dict[str, Union[str, int, float]]]] type seems to cause the error.
semantic_search_result looks like this for example:
[
(0, { "acquisCode": str, "code": str, "level": int, "title": str, "similarity_score": float}),
(1, { "acquisCode": str, "code": str, "level": int, "title": str, "similarity_score": float}),
(2, { "acquisCode": str, "code": str, "level": int, "title": str, "similarity_score": float})
]
When running the code I got the following error:
Traceback (most recent call last):
File "/home/matthieu/Code/Python/fastapi-graphql/test_SQLModel.py", line 6, in <module>
class SemanticSearch(SQLModel, table=True):
File "/home/matthieu/anaconda3/envs/sts-transformers-gpu-fresh/lib/python3.8/site-packages/sqlmodel/main.py", line 292, in __new__
col = get_column_from_field(v)
File "/home/matthieu/anaconda3/envs/sts-transformers-gpu-fresh/lib/python3.8/site-packages/sqlmodel/main.py", line 415, in get_column_from_field
sa_type = get_sqlachemy_type(field)
File "/home/matthieu/anaconda3/envs/sts-transformers-gpu-fresh/lib/python3.8/site-packages/sqlmodel/main.py", line 373, in get_sqlachemy_type
if issubclass(field.type_, str):
TypeError: issubclass() arg 1 must be a class
I got the same error if I tried to define a subclass of the typing_extensions.TypedDict class:
class SemanticSearchDict(TypedDict):
acquis_code: str
code: str
level: int
title: str
similarity_score: float
class SemanticSearch(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
id_user: int
date_time: datetime
query: str
clean_query: str
semantic_search_result: List[Tuple[int, SemanticSearchDict]]
I couldn't find a solution but #57 seems to have the same problem since having the same error.
Operating System
Linux
Operating System Details
Ubuntu 18.04 LTS
SQLModel Version
0.0.4
Python Version
3.8.8
Additional Context
No response
🆙
Up!
different cause it seems, but #121 has a similar error.
same Problem
same problem here!
thank you to the maintainers & contributors of this awesome library!
For anyone who encounters this bug while trying to assign a Literal type, the workaround that I used was defining a separate class of Enum instances:
from enum import Enum
class ValidValues:
class FirstField(str, Enum):
FIRST_VAL = 'First val',
SECOND_VAL = 'Second val'
class SecondField(str, Enum):
FIRST_VAL = 'First val',
SECOND_VAL = 'Second val'
# etc..
class MyTable(SQLModel, table=True):
first_field: ValidValues.FirstField
second_field: ValidValues.SecondField
some_str_field: str
# etc...
Any update on this issue?