[GrandComicsDatabase/gcd-django] Eliminate N+1 queries in character-order display and editing (PR #749)

1 view
Skip to first unread message

Adam Hernandez

unread,
Sep 1, 2026, 8:45:00 PM (3 days ago) Sep 1
to GrandComicsDatabase/gcd-django, Subscribed

Summary

I eliminated the per-character database queries in character-order display and editing flows.

  • Bulk-load character identity and alias relationships once per display/edit operation.
  • Bulk-load character-order through rows instead of calling get() for each appearance.
  • Batch removals, updates, and inserts in the character-order editing view.
  • Preserve ordering and alias/civilian display behavior.

Testing

  • apps/oi/tests/db/test_character_order_revision.py: 7 passed
  • Story/revision compatibility suite: 71 passed
  • Added query-bound regression coverage for display and edit preparation.

Closes #731


You can view, comment on, or merge this pull request online at:

  https://github.com/GrandComicsDatabase/gcd-django/pull/749

Commit Summary

  • b6ae3a8 Eliminate character-order N+1 queries

File Changes

(4 files)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.Message ID: <GrandComicsDatabase/gcd-django/pull/749@github.com>

gemini-code-assist[bot]

unread,
Sep 1, 2026, 8:46:33 PM (3 days ago) Sep 1
to GrandComicsDatabase/gcd-django, Subscribed

@gemini-code-assist[bot] commented on this pull request.

Code Review

This pull request optimizes character and story-character ordering queries by introducing a bulk-loading cache (_build_character_identity_cache) for alias and civilian identities, and by batching database operations (using bulk_create and bulk_update) during character reordering. These changes significantly reduce the number of database queries. However, a critical bug was identified in apps/gcd/models/story.py where querying story_character__story_character_id instead of story_character_id for character revisions causes ID mismatches and breaks the editing flow.


In apps/gcd/models/story.py:

> +    if field == 'character_revisions':
+        through_rows = through_rows.values(
+            'story_character__story_character_id', 'order_code')
+        order_codes = {
+            row['story_character__story_character_id']: row['order_code']
+            for row in through_rows
+        }

high

In the character_revisions branch, through_rows is queried from CharacterThroughOrderRevision (which connects CharacterOrderRevision to StoryCharacterRevision).

Using 'story_character__story_character_id' selects the ID of the production StoryCharacter model. However, all_appearing_characters contains StoryCharacterRevision instances, so member.id is compared against in_character_order_ids using StoryCharacterRevision IDs. Because StoryCharacter IDs and StoryCharacterRevision IDs do not match, the character order matching logic fails completely in the editing flow. Additionally, for newly added characters, story_character_id is None until committed, which would result in None keys.

To fix this, we should query 'story_character_id' directly, which retrieves the StoryCharacterRevision IDs and correctly aligns with member.id.

⬇️ Suggested change
-    if field == 'character_revisions':
-        through_rows = through_rows.values(
-            'story_character__story_character_id', 'order_code')
-        order_codes = {
-            row['story_character__story_character_id']: row['order_code']
-            for row in through_rows
-        }
+    if field == 'character_revisions':
+        through_rows = through_rows.values(
+            'story_character_id', 'order_code')
+        order_codes = {
+            row['story_character_id']: row['order_code']
+            for row in through_rows
+        }


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <GrandComicsDatabase/gcd-django/pull/749/review/5084528103@github.com>

Adam Hernandez

unread,
Sep 1, 2026, 9:10:55 PM (3 days ago) Sep 1
to GrandComicsDatabase/gcd-django, Push

@DeusExTaco pushed 1 commit.

  • bd13b7f Fix revision character-order ID mapping


View it on GitHub or unsubscribe.


Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <GrandComicsDatabase/gcd-django/pull/749/before/b6ae3a879a1c4d52c4565df509178ae98b21969b/after/bd13b7ff1bb85b52ec2753c7b7665b9264bfb265@github.com>

Reply all
Reply to author
Forward
0 new messages