fix: remap card template index using source note id on apkg import - #5210
Draft
krMaynard wants to merge 1 commit into
Draft
fix: remap card template index using source note id on apkg import#5210krMaynard wants to merge 1 commit into
krMaynard wants to merge 1 commit into
Conversation
import_cards remapped a card's template index inside add_card, which runs *after* map_to_imported_note has overwritten card.note_id with the target note id. But notetype_map (and hence remapped_templates) is keyed by the *source* note id, so the lookup missed and the template index was left unchanged whenever a note's id changed on import (id-collision uniquify, or a guid match to a differently-id'd note). Cards could then point at the wrong template, and card_ordinal_already_exists deduped on the un-remapped ordinal. Compute the remapped index in import_cards using the source note id, before map_to_imported_note runs, via a small pure helper. Adds a unit test for the remap lookup.
krMaynard
added a commit
to krMaynard/anki-fork
that referenced
this pull request
Jul 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked issue (required)
Fixes #5209
Summary / motivation (required)
import_cardsremapped a card's template index (ord) insideadd_card, which runs aftermap_to_imported_notehas already overwrittencard.note_idwith the target note id. Butnotetype_map— and thereforeremapped_templates— is keyed by the source note id, so the lookup missed and the template index was left unchanged whenever a note's id changed on import (id-collision uniquify, or a guid match to a differently-id'd note).Consequences: cards could point at the wrong template after import, and
card_ordinal_already_existsdeduplicated on the un-remapped ordinal.The fix computes the remapped index in
import_cardsusing the source note id, beforemap_to_imported_noteruns, via a small pure helper.Steps to reproduce (required, use N/A if not applicable)
.apkgwhere an imported note's id collides with an existing note (forcing uniquify) or matches by guid to a differently-id'd note..apkginto the target collection.How to test (required)
Checklist (minimum)
./ninja checkor an equivalent relevant check locally (via the fork CI run linked below).Details
Adds a unit test for the remap lookup exercising the source-note-id keying. Full CI (
checkon Linux/macOS/Windows,format,minilints) was run against this exact commit on the fork: https://github.com/krMaynard/anki-fork/actions/runs/30167008557 — build, lint, and test pass on all three OSes. (The run shows red only for theUpload SARIF results for complexipystep, which always fails on forks due to a token-permission limitation, not a code failure.)Before / after behavior (optional)
Before: template ordinals silently left un-remapped when a note's id changed on import. After: ordinals remapped using the source note id.
Risk / compatibility / migration (optional)
Low risk; import-path-only change, no schema or scheduling impact.
UI evidence (required for visual changes; otherwise N/A)
N/A
Scope