-
-
Notifications
You must be signed in to change notification settings - Fork 781
Closed
Labels
questionFurther information is requestedFurther information is requested
Description
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the SQLModel documentation, with the integrated search.
- I already searched in Google "How to X in SQLModel" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import List
from sqlalchemy import func
from sqlalchemy import select as sa_select
from sqlalchemy.orm import column_property
from sqlmodel import create_engine, Field, Relationship, SQLModel
class Hero(SQLModel, table=True):
name: str = Field(primary_key=True)
team_name: str = Field(foreign_key="team.name")
team: "Team" = Relationship(back_populates="heroes")
class Team(SQLModel, table=True):
name: str = Field(primary_key=True)
heroes_count = column_property(
sa_select(func.count(Hero.team_name))
.where(Hero.team_name == name)
.correlate_except(Hero)
.scalar_subquery()
)
heroes: List["Hero"] = Relationship(back_populates="team")
if __name__ == "__main__":
engine = create_engine("sqlite:///test.db")
SQLModel.metadata.create_all(engine)Description
- Create a Hero model
- Crate a Team model
- Create a calculated column using
sqlalquemy.orm.column_property(https://docs.sqlalchemy.org/en/14/orm/mapped_sql_expr.html#using-column-property) - Create all tables and it will raise the
sqlalquemy.exc.InvalidRequestError
sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Hero->hero, expression 'Team' failed to locate a name ('Team').
If this is a class name, consider adding this relationship() to the <class '__main__.Hero'> class after both dependent classes have been defined.Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
Python 3.9.6
Additional Context
No response
rootux, StefanBrand, deZakelijke, DanLipsitt, stefanuddenberg and 3 moremartinralfreindl
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested