Given these classes and 1-to-N relationships, I get compile time error: ValueError: <class 'list'> has no matching SQLAlchemy type
referring to the category.py
field todos: list["Todo"]
, while the relation hero/team
does work.
I also tried to replace the UUID
type and use int
in both classes, without success.
# hero.py (works)
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
....
team_id: int | None = Field(default=None, foreign_key="team.id")
team: Team | None = Relationship(back_populates="heroes")
# team.py (works)
class Team(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
.....
heroes: list["Hero"] = Relationship(back_populates="team")
# todo.py (does not work)
class Todo(SQLModel, table=True):
id: UUID | None = Field(
default=uuid4(),
primary_key=True,
)
......
category_id: UUID | None = Field(default=None, foreign_key="category.id")
category: Category | None = Relationship(back_populates="todos")
# category.py (does not work => ValueError: <class 'list'>)
class Category(SQLModel, table=True):
id: UUID | None = Field(default=uuid4(), primary_key=True)
.....
todos: list["Todo"] = Relationship(back_populates="category")
Full error log:
import todo.models.category
File ".../todo/models/category.py", line 9, in <module>
class Category(SQLModel, table=True):
File ".../lib/python3.12/site-packages/sqlmodel/main.py", line 559, in __new__
col = get_column_from_field(v)
^^^^^^^^^^^^^^^^^^^^^^^^
File "../lib/python3.12/site-packages/sqlmodel/main.py", line 708, in get_column_from_field
sa_type = get_sqlalchemy_type(field)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "../lib/python3.12/site-packages/sqlmodel/main.py", line 697, in get_sqlalchemy_type
raise ValueError(f"{type_} has no matching SQLAlchemy type")
ValueError: <class 'list'> has no matching SQLAlchemy type
1
I have check FastAPI docs and found a note:
Notice that the relationship attributes, the ones with Relationship(), are only in the table models, as those are the ones that are handled by SQLModel with SQLAlchemy and that can have the automatic fetching of data from the database when we access them.
class TeamBase(SQLModel):
name: str = Field(index=True)
headquarters: str
class Team(TeamBase, table=True):
id: int | None = Field(default=None, primary_key=True)
heroes: list["Hero"] = Relationship(back_populates="team")
class HeroBase(SQLModel):
name: str = Field(index=True)
secret_name: str
age: int | None = Field(default=None, index=True)
team_id: int | None = Field(default=None, foreign_key="team.id")
class Hero(HeroBase, table=True):
id: int | None = Field(default=None, primary_key=True)
team: Team | None = Relationship(back_populates="heroes")
I think you should try to split your model into two classes: SQLModel for DB fields and table models for relationship and check again. Hope you can fix this!
Hoàng Lê Ngọc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1