INSIDE DJANGO: HOW TO CHANGE PRIMARY KEY
October 10, 2022
Our developer Fabian provides his technical insights from a project for Cornelsen Verlag. The topic: Changing the primary key for existing models in the django framework.
django Guide: Changing the primary key for an existing model
As part of a major data warehouse project for Cornelsen Verlag, we faced a technical challenge some time ago: a significant change to the backend data model was on the horizon. Our development team came up with a solution that we’d like to share here.
The task was to switch from a 22-character field to an internal UUID as the primary key. The 22-character ID was the primary key from an existing source system. The idea was to decouple the two systems so that we could create virtual entries without using a random string, which could have led to errors in the future. At that time, our database table contained over 3.4 million entries. It was connected to about 16 other tables via foreign keys and many-to-many relationships.
After conducting thorough research, I used the solution described in this stackoverflow-answer.
I've posted the complete guide on GitHub.
You can learn more about the project here: Case Study: Workflow Automation in Publishing Management.
Before you apply these steps to your production database, always test them with test data!
Agency for software development with Python & Django
As an agency for Python-development we use the Django-Framework. in numerous projects to develop high-performance web applications—whenever security, flexibility, and a fast time-to-market are key considerations.
If you're running into limitations with your backend, feel free to schedule a meeting with our team!
Fabian Clemenz
[email protected]