TypeError: issubclass() arg 1 must be a class when having a field with a complex type #1522
-
First Check
Commit to Help
Example Codefrom 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) DescriptionI would like to create a The last field
When running the code I got the following error:
I got the same error if I tried to define a subclass of the
I couldn't find a solution but #57 seems to have the same problem since having the same error. Operating SystemLinux Operating System DetailsUbuntu 18.04 LTS SQLModel Version0.0.4 Python Version3.8.8 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
🆙 |
Beta Was this translation helpful? Give feedback.
-
Up! |
Beta Was this translation helpful? Give feedback.
-
different cause it seems, but #121 has a similar error. |
Beta Was this translation helpful? Give feedback.
-
same Problem |
Beta Was this translation helpful? Give feedback.
-
same problem here! |
Beta Was this translation helpful? Give feedback.
-
thank you to the maintainers & contributors of this awesome library! For anyone who encounters this bug while trying to assign a
|
Beta Was this translation helpful? Give feedback.
-
Any update on this issue? |
Beta Was this translation helpful? Give feedback.
-
same happen using UUID4 for id |
Beta Was this translation helpful? Give feedback.
-
Its because of the you can bypass this using
i think dicts need to be typed to json |
Beta Was this translation helpful? Give feedback.
-
You need to specify the column type for SQLAlchemy ( semantic_search_result: List[Tuple[int, Dict[str, Union[str, int, float]]]] = Field( sa_type=JSON ) Runnable code example in the details: from typing import Dict, List, Optional, Tuple, Union from sqlmodel import JSON, Field, Session, SQLModel, create_engine class SemanticSearch(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) ... semantic_search_result: List[Tuple[int, Dict[str, Union[str, int, float]]]] = Field( sa_type=JSON ) ## sqlite sqlite_url = "sqlite:///" engine = create_engine(sqlite_url, echo=True) def main(): SQLModel.metadata.create_all(engine) with Session(engine) as session: ss = SemanticSearch( semantic_search_result=[ (2, {"acquis_code": "asd"}), (1, {"acquis_code": "qwerty"}), ] ) session.add(ss) session.commit() with Session(engine) as session: ss_db = session.get(SemanticSearch, 1) assert ss_db.semantic_search_result[0][0] == 2 assert ss_db.semantic_search_result[0][1]["acquis_code"] == "asd" assert ss_db.semantic_search_result[1][0] == 1 assert ss_db.semantic_search_result[1][1]["acquis_code"] == "qwerty" if __name__ == "__main__": main() |
Beta Was this translation helpful? Give feedback.
You need to specify the column type for SQLAlchemy (
Field(sa_type=JSON)
in this case):Runnable code example in the details: