Skip to content

Commit 8ed4d80

Browse files
dbraendleclaude
andauthored
fix: use subquery loading for Books relationships to prevent DetachedInstanceError (#1279)
All relationship attributes on the Books model (series, comments, languages, authors, tags, data, ratings, publishers, identifiers) used the default lazy loading strategy. This causes sqlalchemy.orm.exc.DetachedInstanceError when Jinja2 templates access these attributes after the SQLAlchemy session has been closed, resulting in intermittent 500 errors. Switching to lazy='subquery' ensures all relationship data is loaded eagerly alongside the main query, preventing any lazy-load attempts on detached instances. Fixes #1067, #1130, #1139 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 33bbf34 commit 8ed4d80

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

cps/db.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ class Books(Base):
404404
has_cover = Column(Integer, default=0)
405405
uuid = Column(String)
406406

407-
authors = relationship(Authors, secondary=books_authors_link, backref='books')
408-
tags = relationship(Tags, secondary=books_tags_link, backref='books', order_by="Tags.name")
409-
comments = relationship(Comments, backref='books')
410-
data = relationship(Data, backref='books')
411-
series = relationship(Series, secondary=books_series_link, backref='books')
412-
ratings = relationship(Ratings, secondary=books_ratings_link, backref='books')
413-
languages = relationship(Languages, secondary=books_languages_link, backref='books')
414-
publishers = relationship(Publishers, secondary=books_publishers_link, backref='books')
415-
identifiers = relationship(Identifiers, backref='books')
407+
authors = relationship(Authors, secondary=books_authors_link, backref='books', lazy='subquery')
408+
tags = relationship(Tags, secondary=books_tags_link, backref='books', order_by="Tags.name", lazy='subquery')
409+
comments = relationship(Comments, backref='books', lazy='subquery')
410+
data = relationship(Data, backref='books', lazy='subquery')
411+
series = relationship(Series, secondary=books_series_link, backref='books', lazy='subquery')
412+
ratings = relationship(Ratings, secondary=books_ratings_link, backref='books', lazy='subquery')
413+
languages = relationship(Languages, secondary=books_languages_link, backref='books', lazy='subquery')
414+
publishers = relationship(Publishers, secondary=books_publishers_link, backref='books', lazy='subquery')
415+
identifiers = relationship(Identifiers, backref='books', lazy='subquery')
416416

417417
def __init__(self, title, sort, author_sort, timestamp, pubdate, series_index, last_modified, path, has_cover,
418418
authors, tags, languages=None):

0 commit comments

Comments
 (0)