-
First Check
Commit to Help
Example Codefrom datetime import datetime, timedelta, timezone
from enum import Enum
from typing import BinaryIO, List, Optional
from uuid import UUID, uuid4
from pydantic import EmailStr
from sqlmodel import JSON, Column, Field, Relationship, SQLModel
class UserRole(int, Enum):
DEFAULT = 0
TEAM_LEADER = 1
MODERATOR = 2
ADMIN = 3
class UserBase(SQLModel):
username: str = Field(index=True, max_length=config.MAX_USERNAME_LEN, min_length=config.MIN_USERNAME_LEN)
email: EmailStr = Field(unique=True)
class User(UserBase, table=True):
__tablename__ = "users" # type: ignore
id: Optional[int] = Field(default=None, primary_key=True)
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
disabled: bool = Field(default=True)
api_key: Optional[str] = Field(default=None, index=True, unique=True)
password: str = Field(max_length=config.MAX_PASSWORD_LENGTH, min_length=config.MIN_PASSWORD_LENGTH)
role: "UserRole" = Field(default=UserRole.DEFAULT)
code: Optional[str] = Field(max_length=config.VERIFICATION_CODE_STR_LEN)
led_team: Optional["Team"] = Relationship(back_populates="leader")
class TeamBase(SQLModel):
team_number: int = Field(index=True, unique=True, ge=0, le=config.MAX_TEAM_NUMB)
team_name: str = Field(max_length=config.MAX_TEAM_NAME_LEN)
class Team(TeamBase, table=True):
__tablename__ = "teams" # type: ignore
id: int | None = Field(default=None, primary_key=True)
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
disabled: bool = Field(default=False)
leader_user: Optional[int] = Field(default=None, foreign_key="users.id", unique=True)
leader: Optional["User"] = Relationship(back_populates="led_team") DescriptionI had a working schema, but wanted to switch to the system described in the docs. I refactored and modified the code to get roughly what I have above. (There are some things missing in my example) The app runs, but when tring to login, I get the following error:
I have tried adding the I hope I am submiting this in the right place, and it is not a SQLAlchemy issue. Thank you! Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.24 Python Version3.10.12 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Could you provide MRE? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I already fixed this, and forgot to close this. I was able to split the schema into multiple files and only build the mappers after every model was imported. I will send my implementation and close this issue in a few hours.
…On Wed, Sep 17, 2025, at 1:22 AM, Motov Yurii wrote:
Could you provide MRE <https://stackoverflow.com/help/minimal-reproducible-example>?
—
Reply to this email directly, view it on GitHub <#1563 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BNBQPEBQLBZN752CHRAQTJT3TD42RAVCNFSM6AAAAACGPNJWR6VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTINBSGQZTANA>.
You are receiving this because you authored the
|
Beta Was this translation helpful? Give feedback.
-
# Generic imports here
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from app.models.team import Team
# Models here
def configure_relationships():
"""Call this after importing models to resolve relationships"""
from sqlalchemy.orm import configure_mappers
configure_mappers()
|
Beta Was this translation helpful? Give feedback.
models
module.led_team: Optional["Team"]
)__init__.py
file and add this:SQLModel.metadata.create_all(engine)
), runconfigure_relationships()
as well.