Debugging "No such note: 0" during mw.col.add_note(...)

I eventually found the problem. It was a problem with my other addon during the note_will_be_added hook. The cause was two-fold:

  1. I was calling mw.col.update_notes() on a new note not yet added
  2. I was duplicating a new note using my code from here which called _load_from_backend_note which also throws the error when id=0

I fixed the first by correctly omitting new notes from being passed to mw.col.update_notes and the second by changing my note duplicate function to this:

def duplicate_note(note: Note) -> Note:
    """
    Duplicate a note with deepcopy, copying everything except the col attribute
    as that needs to identical to the original note
    """
    new_note = type(note).__new__(type(note))

    for k, v in note.__dict__.items():
        if k == "col":
            setattr(new_note, k, note.col)
        else:
            setattr(new_note, k, deepcopy(v))
    return new_note