This is what I did originally. The problem is that when I change a notetype, it causes duplicate_or_empty
to return 0 for existing cards.
A simple example. Say I have a Notetype “MyCustomNoteType”. So I do retrieve it:
notetype = self.col.models.byName("MyCustomNoteType")
Then I try to detect if a Note already exists, so I construct a Note Object with an id as the first field
note = self.col.new_note(notetype)
note["id"] = "This is the first field and a note with such field exists"
I detect a change:
if note.duplicate_or_empty() == DuplicateOrEmptyResult.DUPLICATE:
print("Note exists")
So far this works. But If I change the Nottype in certain ways, it causes duplicte_or_empty() to return 0, so no duplicate.
What I can do:
notetype["tmpls"] = []
self.col.models.add_template(notetype, self.col.models.new_template("Card 1"))
self.col.models.save(notetype)
So remove all templates and add exact the same templates. It works.
What I can’t do
notetype["flds"] = []
field = self.col.models.new_field("id")
self.col.models.add_field(notetype, field)
self.col.models.save(notetype)
So removing all fields and adding exact the same fields.
So I try to search what the difference is: The ordinal numbers are None
But in general: I don’t understand what the notetype has to do with duplicate_or_empty
for the note. If the note doesn’t change (same fields, same values, same id in first field, same notetype Id) that function should return duplicate. If the Notetype gets a new field and the note gets a new field, it should still be a duplicate, because the first field matches.
I figure I might just wanna search myself for this. So something like:
noteIds = self.col.find_notes(self.col.build_search_string("Here my duplicate id", SearchNode(field_name="id")))
if noteIds == []:
print("No Duplicate")
else
print("Duplicate")
Edit: Turns out this doesn’t work either when I modify the notetype in any way.
I think you’re right. I thought as I constructed the note Id with all fields before, I could just copy the id over. But obviously there are more fields like the usn. So yes I should load the original note and modify that.
I’ll see if that also fixes my issue.