Model reprs read only loaded state (#819)

A repr runs inside log calls. Reading a relationship or an expired column there runs a query, and
on an object whose session is gone it raises DetachedInstanceError instead of logging. Archive's
repr also stat'ed its SingleFile file on disk.

`wrolpi.common.loaded()` follows attributes through state that is already loaded (primary keys
from the identity key) and never runs SQL. Every model repr uses it; with everything loaded the
output is unchanged, except Video and Archive now show the FileGroup's primary path.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-10-11 16:47:32 -06:00 • committed by GitHub
parent c717f11821
commit 3ea8eb81f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 154 additions and 52 deletions

View file

@ -5,7 +5,7 @@ from sqlalchemy import Column, Integer, String, ForeignKey, BigInteger, Text, Bo
from sqlalchemy.orm import relationship, Session, joinedload, selectinload, deferred
from wrolpi.collections.models import Collection
from wrolpi.common import Base, ModelHelper, get_relative_to_media_directory
from wrolpi.common import loaded, Base, ModelHelper, get_relative_to_media_directory
from wrolpi.dates import TZDateTime
from wrolpi.db import serializer
from wrolpi.downloader import Download
@ -57,7 +57,8 @@ class Repository(ModelHelper, Base):
uselist=False)
def __repr__(self):
return f'<Repository id={self.id} url={self.url} directory={self.directory}>'
return f'<Repository id={loaded(self, "id")} url={loaded(self, "url")} ' \
f'directory={loaded(self, "collection", "directory")}>'
@property
def name(self) -> Optional[str]: