10 Migration

 

This chapter covers

  • The internals of migration scripts
  • Writing custom migrations, including manipulating existing data
  • Combining and optimizing migration files
  • Dealing with merge conflicts caused by changing a model

Django Model objects are proxies to tables in a database, and the migration system helps you maintain the relationship between the two. The migration system is a powerful tool that allows you to move forward and backward through state changes and manipulate data while doing so.

10.1 Migration scripts in detail

In chapter 4, you learned about Django’s ORM and how Model classes act as a proxy to tables in the database. You used the makemigrations and migrate commands to push changes down to the database that correspond to the code in your model files. Managing changes to your models can be a bit complicated, and this chapter dives deeper into the migration system.

To better understand what happens during the migration process, it is time to create a new Django app. This app won’t have much functionality in it, and for now, it won’t even have any views, but by working with a fresh model file in a new app, you’ll be able to see exactly what happens at each step in the migration process.

10.1.1 An initial migration

10.1.2 Changing a model

10.2 Migration with existing data

10.3 Squashing a migration

10.4 More migration choices

10.5 Exercises

Summary