What is the best way to create a deck and note with a custom ID?

I have an addon that syncs with a server and updates cards based upon their IDs. Currently, I use the deck and note IDs generated from the anki client but I would like to be able to create decks/notes with specified Unix time stamps. Is there a safe way to do this?

I wonder what issue this would solve. Can you perhaps elaborate?

1 Like

I want to generate the cards server side and push them to the anki clients. That requires also generating the IDs server side. Thus I need the ability to create decks and notes using that generated ID.

It seems that Anki simply does insert-and-update?

Yes but there is no exposed python interface to generate a new note or deck with a custom ID as far as I can tell. Do you have a method or is this not possible?

I’m not sure, perhaps someone else knows. But it’s possible to just execute SQL directly. Also I think it should be possible to add a card and change its id right after, e.g. with something like update cards set id=5 where id=1. Not sure if any of this is a good idea but it’s not impossible :s

This functionality is not exposed yet, but is on the todo list to look into.

Awesome! I’d love to get involved if all possible. Let me know how I can help.

This would come as part of larger work on a JSON-based importer, so it’s not something that’s easy to jump into I’m afraid.


I do this:

id = hashlib.md5(front.encode(‘utf-8’)).hexdigest()

For different needs I do the same. I created a simple addon that inserts a 8 Character unique name into a field that I added to the card. When I create a new card I hit a key and the unique id is filled in.

How do you ensure this doesn’t create conflicts? Hashes are not collision-free (they are most of the time, but definitively not always).

I had no problems with the code above.

This is the exit he returns.

The idea is that the ID represents a UNIQUE card to avoid duplication when importing.

Yes I got that, the problem is that hashing is not made to create unique IDs. It’s goal is close enough that it will work most of the time, but there are no guarantee that it won’t generate twice the same value. How do you handle that?

I think by the probability of collisions, it’s not a problem. But I never studied it fully.