Sometimes, you have a well defined structure in your database, but you have to refactor it . This refactor implies hard and sensitive changes that may break your database. that’s where Data migration appears, to facilitate those changes.
“As well as changing the database schema, you can also use migrations to change the data in the database itself, in conjunction with the schema if you want”. There are many useful cases in which we can use Django Migrations, for instance, when we need to add a new slug non-nullable field to a User model. also, if we want to add the fullName field to the User model, fullname would be a concatenation between firstName and lastName, in those cases we need to populate the fields with data that is already in the database. Other use cases are when we need to change a foreign key to Many to Many relationship or vice versa.
Data migrations let you change the data in the database according to the schema. You have to trigger the migration creation manually. Data migrations keeps features of a typical schema migration, such as dependencies to others migrations.
In this post we will cover a simple case in which we want to switch the foreign key between two models, lets see:
The model Client is associated with a foreign key to the Car Model.
We will add the new foreign key to the Car model and after that we will run makemigrations, to automatically create the new field, notice that the foreignKey is associated with a String ‘client.Client’ and not to the model Class and that is because Django allows to set the model using a string to avoid circular imports, which is a good thing.
Now, lets type:
python manage.py makemigrations –empty car
The above command will create a blank migration:
In this new file, we will switch the relationship within RunPython operation.
RunPython is the main operation you use for data migrations
Then we can remove the relationship of car inside Client and generate the migration file automatically.
Finally we must migrate and we will be able see the new data structure.
Data migration is very useful when you need to modify data without losing any information, but it is important to keep a dump of your database before doing this migration, if something happens and the code is not working good during the migration, you will lost information, you will need to import your dump and try again.
We must do data migration with precaution, more than anything when we apply those changes in the production site, so if it is a good idea to take a dump of production site and test it in the staging site. Also if we make the migrations step by step, as we do it in the previous example, we will have more control.