sqlalchemy.exc.CompileError: Can't generate DDL for NullType(); did you forget to specify a type on this Column? #1520
Answered by YuriiMotov
Matthieu-Tinycoaching asked this question in Questions
-
First Check
Commit to Help
Example Codefrom datetime import datetime from typing import Optional, List, Tuple, Dict, Union from typing_extensions import TypedDict from sqlmodel import Field, SQLModel, create_engine from sqlalchemy_utils.functions import database_exists, create_database class InnerSemanticSearchDict(TypedDict): acquis_code: str code: str level: int title: str similarity_score: float class SemanticSearchDict(TypedDict): rank: int value: InnerSemanticSearchDict 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[SemanticSearchDict] ## sqlite sqlite_file_name = "database.db" sqlite_url = f"sqlite:///{sqlite_file_name}" if not database_exists('postgresql://postgres:postgres@localhost:5432/embeddings_sts_tf'): create_database('postgresql://postgres:postgres@localhost:5432/embeddings_sts_tf') engine = create_engine('postgresql://postgres:postgres@localhost:5432/embeddings_sts_tf', echo=True) SQLModel.metadata.create_all(engine) DescriptionI would like to create a But I got the following error code:
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.
Answered by YuriiMotov Aug 14, 2025
Replies: 2 comments
-
Same error with custom types: #42 |
Beta Was this translation helpful? Give feedback.
0 replies
-
You need to specify column type for SQLAlchemy: semantic_search_result: List[SemanticSearchDict] = Field(sa_type=ARRAY(JSON)) Runnable code example in the details: from typing import List, Optional from sqlalchemy_utils.functions import create_database, database_exists from sqlmodel import ARRAY, JSON, Field, Session, SQLModel, create_engine from typing_extensions import TypedDict class InnerSemanticSearchDict(TypedDict): acquis_code: str ... class SemanticSearchDict(TypedDict): rank: int value: InnerSemanticSearchDict class SemanticSearch(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) ... semantic_search_result: List[SemanticSearchDict] = Field(sa_type=ARRAY(JSON)) db_url = "postgresql://postgres:mysecretpassword@localhost:5432/embeddings_sts_tf" def main(): if not database_exists(db_url): create_database(db_url) engine = create_engine(db_url, echo=True) SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) with Session(engine) as session: ss = SemanticSearch( semantic_search_result=[ SemanticSearchDict( rank=2, value=InnerSemanticSearchDict(acquis_code="asd") ), SemanticSearchDict( rank=1, value=InnerSemanticSearchDict(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]["rank"] == 2 assert ss_db.semantic_search_result[0]["value"]["acquis_code"] == "asd" assert ss_db.semantic_search_result[1]["rank"] == 1 assert ss_db.semantic_search_result[1]["value"]["acquis_code"] == "qwerty" if __name__ == "__main__": main() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You need to specify column type for SQLAlchemy:
Runnable code example in the details: