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

@ -4,7 +4,7 @@ from typing import Optional
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, String, Text, Index, or_
from sqlalchemy.orm import Mapped, relationship, Session, joinedload
from wrolpi.common import ModelHelper, Base
from wrolpi.common import loaded, ModelHelper, Base
from wrolpi.db import get_db_session, serializer
from wrolpi.files.models import FileGroup
@ -62,8 +62,8 @@ class Doc(ModelHelper, Base):
passive_deletes=True)
def __repr__(self):
path = str(self.file_group.primary_path) if self.file_group else 'None'
return f'<Doc id={self.id} path={repr(path)} file_group_id={self.file_group_id}>'
path = str(loaded(self, 'file_group', 'primary_path'))
return f'<Doc id={loaded(self, "id")} path={repr(path)} file_group_id={loaded(self, "file_group_id")}>'
@staticmethod
def json_options() -> tuple:
@ -166,4 +166,5 @@ class DocSection(ModelHelper, Base):
doc = relationship('Doc', back_populates='sections')
def __repr__(self):
return f'<DocSection id={self.id} doc_id={self.doc_id} kind={self.kind} ordinal={self.ordinal}>'
return f'<DocSection id={loaded(self, "id")} doc_id={loaded(self, "doc_id")} kind={loaded(self, "kind")} ' \
f'ordinal={loaded(self, "ordinal")}>'