[UPD] Added BookTag
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from .base import Base
|
||||
from .env import Env
|
||||
#from .tag import Tag
|
||||
from .tag import Tag
|
||||
from .path import Path
|
||||
from .library import Library
|
||||
from .book import Book
|
||||
from .book import Book
|
||||
from .book_tag import BookTag
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from sqlalchemy import String, ForeignKey
|
||||
from sqlalchemy import String, ForeignKey, Integer
|
||||
from sqlalchemy.orm import Mapped
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from sqlalchemy.orm import relationship
|
||||
@@ -10,17 +10,22 @@ from .base import Base
|
||||
class Book(Base):
|
||||
__tablename__ = "book"
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
hash: Mapped[str] = mapped_column(String(255))
|
||||
file_name: Mapped[str] = mapped_column(String(255))
|
||||
file_path: Mapped[str] = mapped_column(String(65656))
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
publisher: Mapped[str] = mapped_column(String(255))
|
||||
notes: Mapped[str] = mapped_column(String(65656))
|
||||
classification: Mapped[int] = mapped_column(primary_key=True)
|
||||
hash: Mapped[Optional[str]] = mapped_column(String(255))
|
||||
file_name: Mapped[Optional[str]] = mapped_column(String(65656))
|
||||
file_path: Mapped[Optional[str]] = mapped_column(String(65656))
|
||||
name: Mapped[str] = mapped_column(String(65656))
|
||||
publisher: Mapped[Optional[str]] = mapped_column(String(65656))
|
||||
notes: Mapped[Optional[str]] = mapped_column(String(65656))
|
||||
classification: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
|
||||
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
|
||||
library: Mapped[int] = relationship("Library", back_populates="books")
|
||||
|
||||
tags: Mapped[List["BookTag"]] = relationship(
|
||||
back_populates="book", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
||||
" notes={self.notes!r}, classification={self.classification!r})"
|
||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}, " \
|
||||
f"notes={self.notes!r}, classification={self.classification!r}), " \
|
||||
f"hash={self.hash!r}, file_name={self.file_name}, file_path={self.file_path})"
|
||||
21
app/schema/library/book_tag.py
Normal file
21
app/schema/library/book_tag.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from sqlalchemy import String, ForeignKey
|
||||
from sqlalchemy.orm import Mapped
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import Base
|
||||
|
||||
class BookTag(Base):
|
||||
__tablename__ = "book_tag_relation"
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
|
||||
book_id: Mapped[int] = mapped_column(ForeignKey("book.id"))
|
||||
book: Mapped[int] = relationship("Book", back_populates="tags")
|
||||
|
||||
tag_id: Mapped[int] = mapped_column(ForeignKey("tag.id"))
|
||||
tag: Mapped[int] = relationship("Tag", back_populates="books")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"BookTag(id={self.id!r}, book_id={self.book_id!r}, tag_id={self.tag_id!r}"
|
||||
@@ -25,5 +25,9 @@ class Library(Base):
|
||||
back_populates="library", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
tags: Mapped[List["Tag"]] = relationship(
|
||||
back_populates="library", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r}"
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy import String, ForeignKey
|
||||
from sqlalchemy.orm import Mapped
|
||||
from sqlalchemy.orm import mapped_column
|
||||
from sqlalchemy.orm import relationship
|
||||
@@ -12,10 +12,12 @@ class Tag(Base):
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
|
||||
books: Mapped[List["Book"]] = relationship(
|
||||
back_populates="tags", cascade="all, delete-orphan"
|
||||
books: Mapped[List["BookTag"]] = relationship(
|
||||
back_populates="tag", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
|
||||
library: Mapped[int] = relationship("Library", back_populates="tags")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
||||
" notes={self.notes!r}, classification={self.classification!r})"
|
||||
return f"Tag(id={self.id!r}, name={self.name!r})"
|
||||
Reference in New Issue
Block a user