Skip to content

Looping many to many response in a many to many response in fastapi #5108

@nzivo

Description

@nzivo

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 FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" 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 FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

#user schema
from typing import List, Optional
from app.schemas.RBAC.resource import ResourceInDBBase

from pydantic import BaseModel, EmailStr


# Shared properties
class UserBase(BaseModel):
    email: Optional[EmailStr] = None
    is_active: Optional[bool] = True
    is_superuser: bool = False
    full_name: Optional[str] = None
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    phone: Optional[int] = None
    user_metadata: Optional[str] = None



# Properties to receive via API on creation
class UserCreate(UserBase):
    email: EmailStr
    password: str


# Properties to receive via API on update
class UserUpdate(UserBase):
    password: Optional[str] = None
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    phone: Optional[int] = None
    user_metadata: Optional[str] = None
    is_active: Optional[bool] = True
    is_superuser: bool = None


class UserInDBBase(UserBase):
    id: int
    resources: List[ResourceInDBBase]

    class Config:
        orm_mode = True
        allow_population_by_field_name = True


# Additional properties to return via API
class User(UserInDBBase):
    pass


# Additional properties stored in DB
class UserInDB(UserInDBBase):
    hashed_password: str

#resource schema
from typing import List, Optional
from app.schemas.RBAC.role import RoleInDBBase

from pydantic import BaseModel

# Shared properties
class ResourceBase(BaseModel):
    resource_title: Optional[str] = None
    resource_slug: Optional[str] = None
    resource_description: Optional[str] = None

# Properties to receive via API on creation
class ResourceCreate(ResourceBase):
    resource_title: str
    resource_slug: str
    resource_description: str

# Properties to receive on Resource update
class ResourceUpdate(ResourceBase):
    resource_title: Optional[str] = None
    resource_slug: Optional[str] = None
    resource_description: Optional[str] = None

# Properties shared by models stored in DB
class ResourceInDBBase(ResourceBase):
    id: int
    roles: List[RoleInDBBase]

    class Config:
        orm_mode = True
        allow_population_by_field_name = True

# Properties to return to client
class Resource(ResourceInDBBase):
    pass

# Properties properties stored in DB
class ResourceInDB(ResourceInDBBase):
    pass

#role schema
from typing import List, Optional
from app.schemas.RBAC.permission import PermissionInDBBase

from pydantic import BaseModel

# Shared properties
class RoleBase(BaseModel):
    role_title: Optional[str] = None
    role_slug: Optional[str] = None
    role_description: Optional[str] = None

# Properties to receive via API on creation
class RoleCreate(RoleBase):
    role_title: str
    role_slug: str
    role_description: str

# Properties to receive on Role update
class RoleUpdate(RoleBase):
    role_title: Optional[str] = None
    role_slug: Optional[str] = None
    role_description: Optional[str] = None

# Properties shared by models stored in DB
class RoleInDBBase(RoleBase):
    id: int

    class Config:
        orm_mode = True
        allow_population_by_field_name = True

# Properties to return to client
class Role(RoleInDBBase):
    pass

# Properties properties stored in DB
class RoleInDB(RoleInDBBase):
    pass

Description

I have a fastapi application with 3 entities in the database that have a many to many relationship in a cascading way. Users have many resources and vice versa, resources have many roles and vice versa, I am wondering if it is possible to get a user entry and then list resources attached to it and roles attached to the resource? I have so far managed to display resources for the user but roles returns an empty list,

my schemas are defined in pydantic and sqlachemy for my models

[ { "email": "user@example.com", "is_active": true, "is_superuser": false, "full_name": "string", "first_name": "string", "last_name": "string", "phone": 0, "user_metadata": "string", "id": 0, "resources": [ { "resource_title": "string", "resource_slug": "string", "resource_description": "string", "id": 0, "roles": [] } ] } ]

The error from my docker is as follows
INFO: 172.26.0.6:56558 - "GET /api/v1/users/?skip=0&limit=100 HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in call
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 146, in call
await super().call(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 102, in call
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in call
raise exc from None
File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/starlette/middleware/cors.py", line 76, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in call
raise exc from None
File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in call
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 550, in call
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 204, in app
response_data = await serialize_response(
File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 126, in serialize_response
raise ValidationError(errors, field.type
)
pydantic.error_wrappers.ValidationError: 2 validation errors for User
response -> 0 -> resources -> 0 -> roles
field required (type=value_error.missing)
response -> 2 -> resources -> 0 -> roles
field required (type=value_error.missing)

Operating System

Linux, Windows

Operating System Details

This is a docker container application, the original fullstack mysql fastapi

FastAPI Version

0.54.1

Python Version

3.8

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions