summaryrefslogtreecommitdiffstatshomepage
path: root/src/hircine/db/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/hircine/db/models.py')
-rw-r--r--src/hircine/db/models.py41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/hircine/db/models.py b/src/hircine/db/models.py
index 575771b..5d1a59a 100644
--- a/src/hircine/db/models.py
+++ b/src/hircine/db/models.py
@@ -1,6 +1,6 @@
import os
from datetime import date, datetime, timezone
-from typing import List, Optional
+from typing import Optional
from sqlalchemy import (
DateTime,
@@ -104,12 +104,12 @@ class Archive(MixinID, MixinCreatedAt, MixinOrganized, Base):
cover_id: Mapped[int] = mapped_column(ForeignKey("image.id"))
cover: Mapped["Image"] = relationship(lazy="joined", innerjoin=True)
- pages: Mapped[List["Page"]] = relationship(
+ pages: Mapped[list["Page"]] = relationship(
back_populates="archive",
order_by="(Page.index)",
cascade="save-update, merge, expunge, delete, delete-orphan",
)
- comics: Mapped[List["Comic"]] = relationship(
+ comics: Mapped[list["Comic"]] = relationship(
back_populates="archive",
cascade="save-update, merge, expunge, delete, delete-orphan",
)
@@ -176,37 +176,37 @@ class Comic(
archive_id: Mapped[int] = mapped_column(ForeignKey("archive.id"))
archive: Mapped["Archive"] = relationship(back_populates="comics")
- pages: Mapped[List["Page"]] = relationship(order_by="(Page.index)")
+ pages: Mapped[list["Page"]] = relationship(order_by="(Page.index)")
page_count: Mapped[int]
- tags: Mapped[List["ComicTag"]] = relationship(
+ tags: Mapped[list["ComicTag"]] = relationship(
lazy="selectin",
cascade="save-update, merge, expunge, delete, delete-orphan",
passive_deletes=True,
)
- artists: Mapped[List["Artist"]] = relationship(
+ artists: Mapped[list["Artist"]] = relationship(
secondary="comicartist",
lazy="selectin",
order_by="(Artist.name, Artist.id)",
passive_deletes=True,
)
- characters: Mapped[List["Character"]] = relationship(
+ characters: Mapped[list["Character"]] = relationship(
secondary="comiccharacter",
lazy="selectin",
order_by="(Character.name, Character.id)",
passive_deletes=True,
)
- circles: Mapped[List["Circle"]] = relationship(
+ circles: Mapped[list["Circle"]] = relationship(
secondary="comiccircle",
lazy="selectin",
order_by="(Circle.name, Circle.id)",
passive_deletes=True,
)
- worlds: Mapped[List["World"]] = relationship(
+ worlds: Mapped[list["World"]] = relationship(
secondary="comicworld",
lazy="selectin",
order_by="(World.name, World.id)",
@@ -233,7 +233,7 @@ class Comic(
class Tag(MixinID, MixinModifyDates, MixinName, Base):
description: Mapped[Optional[str]]
- namespaces: Mapped[List["Namespace"]] = relationship(
+ namespaces: Mapped[list["Namespace"]] = relationship(
secondary="tagnamespaces",
passive_deletes=True,
order_by="(Namespace.sort_name, Namespace.name, Namespace.id)",
@@ -356,7 +356,10 @@ class ComicWorld(Base):
def defer_relationship_count(relationship, secondary=False):
- left, right = relationship.property.synchronize_pairs[0]
+ if secondary:
+ left, right = relationship.property.secondary_synchronize_pairs[0]
+ else:
+ left, right = relationship.property.synchronize_pairs[0]
return deferred(
select(func.count(right))
@@ -366,7 +369,23 @@ def defer_relationship_count(relationship, secondary=False):
)
+Comic.artist_count = defer_relationship_count(Comic.artists)
+Comic.character_count = defer_relationship_count(Comic.characters)
+Comic.circle_count = defer_relationship_count(Comic.circles)
Comic.tag_count = defer_relationship_count(Comic.tags)
+Comic.world_count = defer_relationship_count(Comic.worlds)
+
+Artist.comic_count = defer_relationship_count(Comic.artists, secondary=True)
+Character.comic_count = defer_relationship_count(Comic.characters, secondary=True)
+Circle.comic_count = defer_relationship_count(Comic.circles, secondary=True)
+Namespace.tag_count = defer_relationship_count(Tag.namespaces, secondary=True)
+Tag.comic_count = deferred(
+ select(func.count(ComicTag.tag_id))
+ .where(Tag.id == ComicTag.tag_id)
+ .scalar_subquery()
+)
+Tag.namespace_count = defer_relationship_count(Tag.namespaces)
+World.comic_count = defer_relationship_count(Comic.worlds, secondary=True)
@event.listens_for(Comic.pages, "bulk_replace")