One does not simply migrate a primary key! A database adventure from the Django lands.
Web apps usually go through different migrations in their life time. As long as we are in the realm of ORM, migration quests seem rather simple. But once you stumble deeper down the database dungeons, you might face a PK migration monster. This story is for you, because it’s dangerous to go alone!
- timeslot: Monday, 7th April 2025, 10:00-11;00, Room D
- tags: data, web
This story is based on a true encounter. In early 2024 a somewhat seasoned web app questing party (aka. the base dev team at the University of Applied Arts Vienna), ventured into a plot that thickened and got more complicated with every step we thought we are closer to our solution. In a major overhaul of one of our applications we wanted to change the ID of a model from an int
to a ShortUUID
. At first sight this seemed quite simple. Django provided a straightforward migration, that ran nicely in our dev environments. After all an integer could always be easily transformed to a string, and the ShortUUID boils down to a character field in the database. So we could have been done in less then a day. The problem: it worked nicely in some of our dev environments, depending on the test data. But in the end it should work consistently and reversibly on a production system that is already in use since 2018.
After the usual web research it turned out that:
- this problem cannot be solved that simply
- not many people seem to have attempted to do so
So, while we of course asked ourselves if this is really the path we want to go down, we had some good reasons to do so. And instead of half a day, we spent nearly two weeks (including reviews and testing) to solve it. Until today, there remains a grain of doubt whether our solution is the cleanest we could come up with. But we will need it soon again for a similar use case.
To not only spare ourselves the perils of corrupting some production data in the future, but to put out some helpful signs for other questing parties arriving at these primary key migration crossroads, we want to share and discuss this story with the broader community of backend explorers, web appventurers, and database dungeon crawlers.
Andrea Ida Malkah Klaura (IRL: jackie) has degrees in technical computer science from the TU Wien, in science and technology studies from the Univ. of Vienna and in IT security from the FH Technikum Wien. Besides hir current work as open source engineer and coding lecturer at the Univ. of Applied Arts Vienna as well as feminist technoscience studies lecturer at the TU Wien, ze also builds on a background in IT operations. Hir main interest is bringing people and technologies together to create safer, more participatory and sustainable spaces for the adoption and creation of tools and techniques and the sharing of knowledge, with the aim to contribute to a good life for everyone. #morefunnotprofit
