Skip to content

get_filtered_book missing eager-load for custom_column_* relationships — DetachedInstanceError when editing book metadata #1536

Description

@captain-marlow

Describe the bug

When saving book metadata via the edit form, the following error appears if a concurrent CWA background operation (e.g. the ingest watcher triggering a db reconnect) fires during the request:

Error editing book: Parent instance <Books at 0x...> is not bound to a Session;
lazy load operation of attribute 'custom_column_1' cannot proceed

Steps to reproduce

  1. Have one or more custom columns defined in the Calibre library (e.g. "Translators", "Editors")
  2. Open a book's edit page and save metadata
  3. If a db reconnect fires concurrently (ingest watcher, reconnect-db endpoint, etc.), the error occurs

Root cause

get_filtered_book() in cps/db.py uses joinedload to eagerly load all standard relationships (authors, tags, data, series, etc.) specifically to guard against this detached-instance scenario. However, the dynamically-registered custom_column_* relationships are not included in that list. When the session is invalidated before edit_all_cc_data() accesses book.custom_column_1, the lazy-load fails.

Suggested fix

Dynamically add joinedload for each registered custom column relationship in get_filtered_book():

for cc_id in cc_classes:
    cc_rel = getattr(Books, "custom_column_" + str(cc_id), None)
    if cc_rel is not None:
        load_opts.append(joinedload(cc_rel))

Environment

  • CWA image: crocodilestick/calibre-web-automated:latest
  • Custom columns: text type (but affects all types since all use relationships)

Workaround

Retrying the Save on a book edit works — the error is non-destructive (no data is lost).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions