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
- Have one or more custom columns defined in the Calibre library (e.g. "Translators", "Editors")
- Open a book's edit page and save metadata
- 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).
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:
Steps to reproduce
Root cause
get_filtered_book()incps/db.pyusesjoinedloadto eagerly load all standard relationships (authors,tags,data,series, etc.) specifically to guard against this detached-instance scenario. However, the dynamically-registeredcustom_column_*relationships are not included in that list. When the session is invalidated beforeedit_all_cc_data()accessesbook.custom_column_1, the lazy-load fails.Suggested fix
Dynamically add
joinedloadfor each registered custom column relationship inget_filtered_book():Environment
crocodilestick/calibre-web-automated:latestWorkaround
Retrying the Save on a book edit works — the error is non-destructive (no data is lost).