FastAPI OpenAPI schema raises error on unhashable type FieldInfoMetadata in SQLModel 0.0.32 #1737
-
First Check
Commit to Help
Example Codefrom typing import Annotated
from fastapi import Depends, FastAPI
from sqlmodel import Field, SQLModel
app = FastAPI()
class Model(SQLModel):
field: str = Field('default')
@app.get("/")
async def root(
model: Annotated[Model, Depends()],
):
return {"message": "Hello World"}Description
Operating SystemLinux, macOS Operating System DetailsNo response SQLModel Version0.0.32 Python Version3.12.12 Additional Contextfastapi 0.128.0 Using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You shouldn't use Pydantic or SQLModel models with async def root(
model: Annotated[Model, Depends()], # This is wrong
):To declare Query parameters using Pydantic\SQLModel model, use the following approach: async def root(
model: Annotated[Model, Query()],
):Note that it's not currently possible to combine multiple models to declare Query parameters (or combine Query model with single Query parameters). As a workaround you can create a model that inherits from multiple models: class Model(SQLModel):
field: str = Field('default')
class Model2(SQLModel):
field2: str = Field('default')
class QueryParametersModel(Model, Model2):
field3: str
@app.get("/")
async def root(
model: Annotated[QueryParametersModel, Query()],
):This workaround will only work if there are no duplicated field names in combined models.. |
Beta Was this translation helpful? Give feedback.
You shouldn't use Pydantic or SQLModel models with
Depends(this is not supported officially):To declare Query parameters using Pydantic\SQLModel model, use the following approach:
Note that it's not currently possible to combine multiple models to declare Query parameters (or combine Query model with single Query parameters). As a workaround you can create a model that inherits from multiple models: