sqlmodel: FastAPI and Pydantic - Relationships Not Working
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, Optional
from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select
class TeamBase(SQLModel):
name: str
headquarters: str
class Team(TeamBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
heroes: List["Hero"] = Relationship(back_populates="team")
class TeamCreate(TeamBase):
pass
class TeamRead(TeamBase):
id: int
class TeamUpdate(SQLModel):
id: Optional[int] = None
name: Optional[str] = None
headquarters: Optional[str] = None
class HeroBase(SQLModel):
name: str
secret_name: str
age: Optional[int] = None
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
class Hero(HeroBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
team: Optional[Team] = Relationship(back_populates="heroes")
class HeroRead(HeroBase):
id: int
class HeroCreate(HeroBase):
pass
class HeroUpdate(SQLModel):
name: Optional[str] = None
secret_name: Optional[str] = None
age: Optional[int] = None
team_id: Optional[int] = None
class HeroReadWithTeam(HeroRead):
team: Optional[TeamRead] = None
class TeamReadWithHeroes(TeamRead):
heroes: List[HeroRead] = []
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def get_session():
with Session(engine) as session:
yield session
app = FastAPI()
@app.on_event("startup")
def on_startup():
create_db_and_tables()
@app.post("/heroes/", response_model=HeroRead)
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
db_hero = Hero.from_orm(hero)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
return db_hero
@app.get("/heroes/", response_model=List[HeroRead])
def read_heroes(
*,
session: Session = Depends(get_session),
offset: int = 0,
limit: int = Query(default=100, lte=100),
):
heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
return heroes
@app.get("/heroes/{hero_id}", response_model=HeroReadWithTeam)
def read_hero(*, session: Session = Depends(get_session), hero_id: int):
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
return hero
@app.patch("/heroes/{hero_id}", response_model=HeroRead)
def update_hero(
*, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate
):
db_hero = session.get(Hero, hero_id)
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
hero_data = hero.dict(exclude_unset=True)
for key, value in hero_data.items():
setattr(db_hero, key, value)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
return db_hero
@app.delete("/heroes/{hero_id}")
def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
session.delete(hero)
session.commit()
return {"ok": True}
@app.post("/teams/", response_model=TeamRead)
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
db_team = Team.from_orm(team)
session.add(db_team)
session.commit()
session.refresh(db_team)
return db_team
@app.get("/teams/", response_model=List[TeamRead])
def read_teams(
*,
session: Session = Depends(get_session),
offset: int = 0,
limit: int = Query(default=100, lte=100),
):
teams = session.exec(select(Team).offset(offset).limit(limit)).all()
return teams
@app.get("/teams/{team_id}", response_model=TeamReadWithHeroes)
def read_team(*, team_id: int, session: Session = Depends(get_session)):
team = session.get(Team, team_id)
if not team:
raise HTTPException(status_code=404, detail="Team not found")
return team
@app.patch("/teams/{team_id}", response_model=TeamRead)
def update_team(
*,
session: Session = Depends(get_session),
team_id: int,
team: TeamUpdate,
):
db_team = session.get(Team, team_id)
if not db_team:
raise HTTPException(status_code=404, detail="Team not found")
team_data = team.dict(exclude_unset=True)
for key, value in team_data.items():
setattr(db_team, key, value)
session.add(db_team)
session.commit()
session.refresh(db_team)
return db_team
@app.delete("/teams/{team_id}")
def delete_team(*, session: Session = Depends(get_session), team_id: int):
team = session.get(Team, team_id)
if not team:
raise HTTPException(status_code=404, detail="Team not found")
session.delete(team)
session.commit()
return {"ok": True}
Description
Is realationships working for anyone? I either get null or an empty list.
OK, so, I’ve copied the last full file preview at the - https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/ Run it and it creates the Db and the foreign key Then I’ve insert the data into the Db.
Checking the docs UI everything looks great

But when I do a request for a hero, team is null

Really not sure what going on, especially when all I have just is copied the code example with no changes?
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.8.2
Additional Context
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 17 (3 by maintainers)
Commits related to this issue
- Fix SQLalchemy error related to https://github.com/tiangolo/sqlmodel/issues/37 — committed to rochacbruno/dundie-rewards by rochacbruno 2 years ago
- build(api): pin sqlachemy version for compat With sqlmodel See https://github.com/tiangolo/sqlmodel/issues/37 — committed to naturalsolutions/ecoSecrets by deleted user 2 years ago
Have a look at: #315
Pinning
SQLAlchemy = ">=1.4.17,<=1.4.35"helped me!Still having the same issue, here is my case:
What I get in the request response
What I get in the terminal with the
print(r[0].professionals)I too am having the exact same issue regarding backrefs not displaying data. Both with my own app, and the demo above.
I followed your example too @obassett with a failed outcome. Data was created and linked (according to dbeaver), however no data is being displayed with a API call is requested.
Tested Versions on 0.0.3 and 0.0.4 Tested DBs: Sqlite, Mariadb Python: 3.9.6 OS: Fedora KDE
Edit: added screenshots
This worked @Chunkford, I have submitted a pull request #48 to change the pip requirement for the project to be 0.68.1 as it was set to 0.68.0.
I had the same error using sqlmodel with SQLAlchemy 1.4.40…
user: "User" = Relationship(back_populates="person")\\person: Person = Relationship(back_populates="user")During the creation process, if User need to referenceperson_id = int = Field(foreign_key="person.id")it will fail. Even if I commit the changes and refresh the instance, the callback will be always null.How to reproduce: Use sqlmodel=0.0.6 with SQLAlchemy=1.4.40 Using SQLAlchemy 1.4.35 works flawlessly
I had the same problem but using an async engine. I solved it using selectinload as suggested here https://github.com/tiangolo/sqlmodel/issues/74
response = await db_session.exec(select(self.model).where(self.model.id == id).options(selectinload(‘*’)))
OK, worked out what it was. I was using
fastapi==0.68.0There’s a newer versionfastapi==0.68.1that added support forread_with_orm_mode- https://github.com/tiangolo/fastapi/releases/tag/0.68.1@alucarddelta update fastapi
pip install fastapi -Uand it should resolve the issueThanks for the discussion everyone! Yep, for the original problem, you should use the latest FastAPI, at least
0.68.1.For the other issues, it was indeed reported in https://github.com/tiangolo/sqlmodel/issues/315, it was solved in https://github.com/tiangolo/sqlmodel/pull/322, and it will be available in the next version, released in the next hours, SQLModel
0.0.7. 🚀