1

I'm trying to build a Python FastAPI blog system using SQLAlchemy with SQLite and am having problems using/understanding the response_model parameter of the API decorator. Here's a SQLAlchemy model:

class User(SqlAlchemyBase):
__tablename__ = 'user'
__table_args__ = {"keep_existing": True}

user_uid: int = sa.Column(GUID, primary_key=True, default=GUID_DEFAULT_SQLITE)
first_name: str = sa.Column(sa.String)
last_name: str = sa.Column(sa.String)
email: str = sa.Column(sa.String, index=True, unique=True)
posts: List["Post"] = relationship("Post", backref="user")
created: datetime = sa.Column(sa.DateTime, default=datetime.now(tz=timezone.utc), index=True)
updated: datetime = sa.Column(sa.DateTime, default=datetime.now(
    tz=timezone.utc), onupdate=datetime.now(tz=timezone.utc), index=True)

Here's the Pydantic schema's for a User:

from datetime import datetime
from pydantic import BaseModel

class UserBase(BaseModel):
    first_name: str
    last_name: str
    email: str

class UserInDB(UserBase):
    user_uid: int
    created: datetime
    updated: datetime

    class Config:
        orm_mode = True

class User(UserInDB):
    pass

Here's a ULR endpoint to get a single user that has the response_model parameter included:

from typing import List
import fastapi
from starlette import status
from backend.schema.user import User
from backend.services import user_service

router = fastapi.APIRouter()

@router.get("/api/users/{user_uid}", response_model=User, status_code=status.HTTP_200_OK)
async def get_one_user(user_uid: str = None) -> User:
    return await user_service.get_user(user_uid)

If I execute the above call in the OpenAPI docs created by FastAPI I get an internal server error in the OpenAPI docs and these errors in the console that's running the FastAPI application:

File "/Users/dougfarrell/projects/blog/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 137, in serialize_response
    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 6 validation errors for User
response -> first_name
  field required (type=value_error.missing)
response -> last_name
  field required (type=value_error.missing)
response -> email
  field required (type=value_error.missing)
response -> user_uid
  field required (type=value_error.missing)
response -> created
  field required (type=value_error.missing)
response -> updated
  field required (type=value_error.missing)

If I take the response_model parameter out of the @router.get(...) decorator I get back the results of the SQLAlchemy query, but no Pydantic serialization. I don't know what the error stack trace is trying to tell me. Can anyone offer some suggestions, advice or resources that might help me figure out what I'm doing wrong?

I'm using Python version 3.10.1, FastAPI version 0.70.1 and SQLAlchemy version 1.4.29.

Thanks!

2
  • You might get some ideas from github.com/gordthompson/fastapi-tutorial-aiosqlite Commented Jan 8, 2022 at 16:50
  • I can't try it right now but is orm_mode from UserInDB passed to a class that inherite from it ? I have always use it in the exact class i use as output/model validation from orm Commented Jan 8, 2022 at 17:53

1 Answer 1

1

Try this:

from typing import List

and change response_model=User to:

response_model=List[User]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.