1

I need help using SQLModel and FastAPI:

from typing import Optional, List
from sqlalchemy import UniqueConstraint
from sqlmodel import Field, Relationship, SQLModel


class Servers(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    servername: str
    server_ip: str
    is_active: bool

    folders: Optional[List['ServersFolders']] = Relationship(back_populates='server')

    __table_args__ = (
        UniqueConstraint('servername', name='_servername_uc'),
        UniqueConstraint('server_ip', name='_serverip_uc'),
    )


class ServersFolders(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    server_id: int = Field(index=True, foreign_key='servers.id')
    folder_path: str

    server: Servers = Relationship(back_populates='folders')
    account_base_folders: Optional[List['AccountBaseFolders']] = Relationship(
        back_populates='server_folder'
    )

    __table_args__ = (
        UniqueConstraint('server_id', 'folder_path', name='_server_folder_uc'),
    )

But openapi 3.0.2 schema looks like:

http://localhost:8000/docs#

Servers{
    id  integer
    servername* string
    server_ip*  string
    is_active*  boolean
}

ServersFolders{
    id  integer
    server_id*  integer
    folder_path*    string
}

Why schema is not showing "folders" in servers? servers should show folders as a list of serversfolders.

Sr Momo
  • 11
  • 2
  • Please do not include code and other elements as images - use text where appropriate. Images are generally not accessible, not searchable and not copy and pastable for testing code. It also makes it hard to see both the code and the result at the same time, leading to navigating back and forth. – MatsLindh Mar 11 '22 at 14:26
  • You can wrap your code (and schema example) in three backticks \`\`\`code\`\`\` to format it properly or indent each line with four spaces in front. You can also use the code button in the editor to indent the whole code section by selecting it and clicking the code button. – MatsLindh Mar 11 '22 at 14:34
  • I edith it, adding the code, without the image – Sr Momo Mar 11 '22 at 14:36

0 Answers0