kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
1.99k stars 150 forks source link

Why does genanki require both the model id and deck id to be hardcoded? #49

Closed jorgebg closed 4 years ago

jorgebg commented 4 years ago

First of all, thank you for this awesome tool.

I have a few questions about the model id and deck id behavior. According the README, the model id must be randomly generated and then hardcoded into the source code:

You need to pass a model_id so that Anki can keep track of your model. It's important that you use a unique model_id for each Model you define. Use random.randrange(1 << 30, 1 << 31) to generate a suitable model_id, and hardcode it into your Model definition.

Same for the deck id:

Once again, you need a unique deck_id that you should generate once and then hardcode into your .py file.

After reading this, I have the following questions:

How do the model and deck ids work in Anki? Can the model id be unique per deck? What if my deck id collides with another deck already imported in my Anki profile?

Also, why does the id need to be hardcoded? Can the change the deck id change on future versions? What would happen in that case?

remiberthoz commented 4 years ago

Hello! I use genanki to manage my deck, and asked similar questions before, but about the Notes guid.

I did a bit of testing with mock decks on a trial Anki install. Quoting this comment:

As far as I can tell, the guid is the only value that is used to determine if imported cards are news or updates. Note ID and Card ID are internal to each Anki installation:

  • they have to remain unique in each installation

  • when importing, Anki will ensure there are no duplicates by changing the imported ID when necessary

  • the internal IDs of the database inside the .pkg file, have to be consistent from cards to notes (this is managed on creation through genanki scripts)

I also remember that whenever Anki sees a new model_id, it will create a new model in it's database. If the name collides, then Anki will append a small random uuid to the name of the newly imported model.

Same goes for the deck_id. When Anki imports a deck with an ID that was never seen before, it will create a blank new deck and import cards in it (even if cards guid are the same as in another deck).


I hope this is a start to answers. We could do more tests to determine exactly what is important to Anki.

jorgebg commented 4 years ago

Thank you so much for the info, @remiberthoz!

kerrickstaley commented 4 years ago

@remiberthoz is right here. The reason you can't randomly generate a deck ID, or use some other solution like a single global deck ID for all genanki users or choose deck ID by hashing deck properties, is that if the deck ID changes it will result in a new deck being created when you import the generated .apkg file a second time, rather than updating the existing deck. The same is true for models. There's no simple, reliable way to abstract this away from the user.

jorgebg commented 4 years ago

Thanks for the clarification, @kerrickstaley