-
First Check
Commit to Help
Example Codefrom typing import Optional
from sqlmodel import Field, SQLModel
class CropStage(SQLModel, table=True):
"""The values that described the crop calendar months stages.
The dataSQLModel is using shortcuts to make it more readable. Each one describe a specific stage of the plant
"""
__tablename__ = "crop_stage"
id: Optional[int] = Field(default=None, primary_key=True)
short: str = Field(primary_key=True)
"2 letter contraction of the stage name"
name: str
"Full name of the calendar value"
info: Optional[str]
"Specific information on the crop stage"
from sqlmodel import create_engine, SQLModel, Session
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)
# fill the table with all the value for crop stage
stages = [
CropStage(short="pr", name="Pre-Planting"),
CropStage(short="so", name="Sowing"),
]
with Session(engine) as session:
for stage in stages:
session.add(stage)
print(stage)
session.commit() DescriptionIt's the first time I use the lib so there is a very high chance the mistake is coming from my side. I followed the tutorial and slightly adapted it to my need. For the classCropStage, I would like to have 3 columns id: Optional[int] = Field(default=None, primary_key=True) The problem is raised when I try to commit at the end of the session as the database is complaining that the id is not set:
I thought the autoincrement stage was part of sqlmodel, was I wrong ? Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.19 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found the problem but I don't know if it should be considered as a bug or a misusage. |
Beta Was this translation helpful? Give feedback.
I found the problem but I don't know if it should be considered as a bug or a misusage.
My class has 2
primary_key
as I change y mind during development. Removing the field forshort
made it work.