Help needed! sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper[User(user)], expression "relationship('list[RefreshToken]')" seems to be using a generic class #1412
-
First Check
Commit to Help
Example Code# user_model.py
from __future__ import annotations
import uuid
from typing import TYPE_CHECKING
from pydantic import EmailStr # noqa: TC002
from sqlmodel import Field
from sqlmodel import Relationship
from sqlmodel import SQLModel
if TYPE_CHECKING:
from my_app.models.token_model import RefreshToken
class UserBase(SQLModel):
email: EmailStr = Field(unique=True, index=True, max_length=255)
is_active: bool = Field(default=True)
is_superuser: bool = Field(default=False)
full_name: str | None = Field(default=None, max_length=255)
class UserCreate(UserBase):
password: str = Field(min_length=8, max_length=128)
class UserUpdate(SQLModel):
email: EmailStr | None = None
full_name: str | None = None
is_active: bool | None = None
is_superuser: bool | None = None
password: str | None = None
class UserUpdateMe(SQLModel):
full_name: str | None = None
email: EmailStr | None = None
class UserUpdatePasswordMe(SQLModel):
current_password: str = Field(min_length=8, max_length=128)
new_password: str = Field(min_length=8, max_length=128)
class User(UserBase, table=True):
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
hashed_password: str = Field()
refresh_tokens: list[RefreshToken] = Relationship(
back_populates="user", cascade_delete=True
)
class UserPublic(UserBase):
id: uuid.UUID
class UsersPublic(SQLModel):
data: list[UserPublic] = Field(default_factory=list)
count: int = Field(default=0)
# token_model.py
from __future__ import annotations
import uuid # noqa: TC003
from datetime import datetime # noqa: TC003
from typing import TYPE_CHECKING
from sqlmodel import Field
from sqlmodel import Relationship
from sqlmodel import SQLModel
if TYPE_CHECKING:
from my_app.models.user_model import User
class TokenPayload(SQLModel):
sub: str | None = None
class Token(SQLModel):
access_token: str
refresh_token: str
token_type: str = "bearer"
class RefreshToken(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
token: str = Field(index=True, unique=True)
user_id: uuid.UUID = Field(foreign_key="user.id")
expires_at: datetime
user: User = Relationship(back_populates="refresh_tokens") DescriptionI am trying to make a user 1-t relation with refreshtokens. I have followed the example of SQLModel but when I start the app I get this error: sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper[User(user)], expression "relationship('list[RefreshToken]')" seems to be using a generic class as the argument to relationship(); please state the generic argument using an annotation, e.g. "refresh_tokens: Mapped[list['RefreshToken']] = relationship()" I have tried to add the Mapped, remove type_checking block, rebuild the model... None of them has worked. Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.24 Python VersionPython 3.13.5 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There is a known issue with |
Beta Was this translation helpful? Give feedback.
There is a known issue with
from __future__ import annotations
.Try removing it (use
refresh_tokens: list["UserPublic"]
anduser: "User"
)