#๐ SQLModel recursive pydantic repr error
8 messages ยท Page 1 of 1 (latest)
@late kite
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Before migration (using just sqlalchemy):
class Calendar(BaseModel):
__tablename__ = "calendars"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), server_onupdate=func.now())
season_id: Mapped[int] = mapped_column(Integer, ForeignKey("seasons.id"), unique=True)
season: Mapped[Season] = relationship(back_populates="calendar")
events: Mapped[list[Event]] = relationship(back_populates="calendar")
class Event(BaseModel):
__tablename__ = "events"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), server_onupdate=func.now())
name: Mapped[str] = mapped_column(String)
datetime: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
is_time_set: Mapped[bool] = mapped_column(Boolean)
calendar_id: Mapped[int] = mapped_column(Integer, ForeignKey("calendars.id"), index=True)
calendar: Mapped[Calendar] = relationship(back_populates="events")
sessions: Mapped[list[Session]] = relationship(back_populates="event")
And now with sqlmodel:
class Calendar(SQLModel, table=True):
__tablename__ = "calendars"
id: Optional[int] = Field(primary_key=True, default=None)
created_at: datetime = Field(sa_column_kwargs={"server_default": func.now()})
updated_at: datetime = Field(sa_column_kwargs={"server_default": func.now(), "server_onupdate": func.now()})
season_id: int = Field(foreign_key="seasons.id", unique=True)
season: Season = Relationship(back_populates="calendar")
events: list[Event] = Relationship(back_populates="calendar")
class Event(SQLModel, table=True):
__tablename__ = "events"
id: Optional[int] = Field(primary_key=True, default=None)
created_at: datetime = Field(sa_column_kwargs={"server_default": func.now()})
updated_at: datetime = Field(sa_column_kwargs={"server_default": func.now(), "server_onupdate": func.now()})
name: str = Field()
datetime: Optional[datetime] = Field(nullable=True)
is_time_set: bool = Field()
calendar_id: int = Field(foreign_key="calendars.id", index=True)
calendar: Calendar = Relationship(back_populates="events")
sessions: list[Session] = Relationship(back_populates="event")
Logs / traceback: https://paste.pythondiscord.com/HNUA#
I am migrating my ORM from sqlalchemy to sqlmodel, and am running into an error, looks like it with Event and some other table- possibly Calendar, where the pydantic __repr__ is running into issues due to the circular nature of the relationship between the tables (one-to-many). I would've thought this is an incredibly common pattern but I can't spot what I'm doing wrong. Can anyone help?
@late kite
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ SQLModel recursive pydantic repr error