You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLModel关联关系序列化优化:如何避免重复定义响应模型?

SQLModel + FastAPI 关联模型序列化优化方案

问题场景

在SQLModel与FastAPI中定义了一对双向关联的Search和Video模型,但使用Video作为接口响应模型时,返回结果不会包含嵌套的Search关联数据;单独创建VideoResponse模型虽能实现嵌套返回,但存在代码冗余。需求是找到更简洁的实现方式,并了解能否让SQLModel默认序列化关联关系。

原模型与接口代码

from sqlmodel import SQLModel, Field, Relationship
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
import deps

router = APIRouter()

class Search(SQLModel, table=True):
    id: int = Field(default=None, nullable=True, primary_key=True, index=True)
    videos: list["Video"] = Relationship(back_populates="search")

class Video(SQLModel, table=True):
    id: int = Field(default=None, nullable=True, primary_key=True, index=True)
    search_id: int = Field(foreign_key="search.id")
    search: "Search" = Relationship(back_populates="videos")

@router.get("/", response_model=Video)
def read_videos(db: Session = Depends(deps.get_db)):
    return db.query(Video).first()

冗余的响应模型实现

class VideoResponse(SQLModel):
    id: int
    search_id: int
    search: Search

简洁解决方案

1. 利用FastAPI的response_model_include参数

无需新建响应模型,直接在路由中指定需要包含的字段(包括关联字段):

from sqlalchemy.orm import joinedload

@router.get("/", response_model=Video, response_model_include={"id", "search_id", "search"})
def read_videos(db: Session = Depends(deps.get_db)):
    # 必须主动加载关联数据,避免SQLAlchemy延迟加载导致序列化失败
    return db.query(Video).options(joinedload(Video.search)).first()

2. 基于原模型继承扩展响应模型

如果需要复用响应模型定义,通过继承减少冗余:

class VideoResponse(Video):
    search: Search  # 仅补充声明需要序列化的关联字段

@router.get("/", response_model=VideoResponse)
def read_videos(db: Session = Depends(deps.get_db)):
    return db.query(Video).options(joinedload(Video.search)).first()

关于SQLModel默认序列化关联关系

SQLModel默认不会序列化Relationship字段,因为这类字段属于ORM层的关联逻辑,并非数据库表的直接字段(无对应Field定义)。

若想全局默认包含关联字段,可创建基类配置序列化规则,但不推荐:

  • 易引发循环引用问题(比如Search的videos与Video的search互相引用,导致序列化无限递归)
  • 会强制所有接口返回关联数据,违背按需返回的设计原则

建议根据接口实际需求,通过response_model_include或继承扩展的方式灵活控制序列化字段。

内容的提问来源于stack exchange,提问作者Jaknowy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 14:15:08