How to prevent mySQL autoincrement from resetting in Django

I am creating a Django app where the primary keys are AutoFields. i.e. I am not manually assigning any field as primary key in my models.

I need to use mySQL.

I will need to export all the data to excel or perhaps another django app from time to time. Therefore the primary keys must be unique to be able to identify new records or records to be deleted in excel/other app.

However, I have read that mySQL autoincrement counter resets to the max key when database restarts. This will result in reassignment of keys if the latest records were deleted.

I need to avoid this. No key should be reassigned.

How can this be done?

Answer

MySQL 8.0 now keeps the last auto-increment per table persistently. So it remembers between restarts, and does not reset the auto-increment.

https://www.percona.com/blog/2018/10/08/persistence-of-autoinc-fixed-in-mysql-8-0/

Leave a Reply

Your email address will not be published. Required fields are marked *