Skip to content

Sqlmodel 0.0.14 throws strange Pydantic error for relationships when adding another field #723

Answered by nazmi
junoriosity asked this question in Questions
Discussion options

You must be logged in to vote

If you want to inherit from a model, starts with a Base that shares common attributes.

from typing import List, Optional

from sqlmodel import Field, Relationship, SQLModel

class HeroBase(SQLModel):
    name: str
    secret_name: str
    age: Optional[int] = None

class Hero(HeroBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    teams: List[Team] = Relationship(back_populates="heroes")

class HeroRead(HeroBase):
    id: int
    is_new: bool = False

Only the ones with table==True are created. You should put is_new inside Hero, otherwise, this attribute isn't in the database.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@junoriosity
Comment options

Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants