mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
spliting models
This commit is contained in:
@@ -6,16 +6,36 @@ class AuthorBookLink(SQLModel, table=True):
|
||||
author_id: int | None = Field(default=None, foreign_key="author.id", primary_key=True)
|
||||
book_id: int | None = Field(default=None, foreign_key="book.id", primary_key=True)
|
||||
|
||||
# Author model
|
||||
class Author(SQLModel, table=True):
|
||||
id: int | None = Field(primary_key=True, index=True)
|
||||
# Author DTO model
|
||||
class AuthorBase(SQLModel):
|
||||
name: str
|
||||
|
||||
class Config: # pyright: ignore
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"name": "author_name",
|
||||
}
|
||||
}
|
||||
|
||||
# Author DB model
|
||||
class Author(AuthorBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True, index=True)
|
||||
books: List["Book"] = Relationship(back_populates="authors", link_model=AuthorBookLink)
|
||||
|
||||
# Book model
|
||||
class Book(SQLModel, table=True):
|
||||
id: int | None = Field(primary_key=True, index=True)
|
||||
# Book DTO model
|
||||
class BookBase(SQLModel):
|
||||
title: str
|
||||
description: str
|
||||
|
||||
class Config: # pyright: ignore
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"title": "book_title",
|
||||
"description": "book_description",
|
||||
}
|
||||
}
|
||||
|
||||
# Book DB model
|
||||
class Book(BookBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True, index=True)
|
||||
authors: List[Author] = Relationship(back_populates="books", link_model=AuthorBookLink)
|
||||
|
||||
Reference in New Issue
Block a user