Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Django make migrations for help_text and verbose_name changes?

When I change help_text or verbose_name for any of my model fields and run python manage.py makemigrations, it detects these changes and creates a new migration, say, 0002_xxxx.py.

I am using PostgreSQL and I think these changes are irrelevant to my database (I wonder if a DBMS for which these changes are relevant exists at all).

Why does Django generate migrations for such changes? Is there an option to ignore them?

Can I apply the changes from 0002_xxxx.py to the previous migration (0001_initial.py) manually and safely delete 0002_xxxx.py?

Is there a way to update previous migration automatically?

like image 866
utapyngo Avatar asked Oct 22 '14 09:10

utapyngo


People also ask

Why does Django migration?

Django will make migrations for any change to your models or fields - even options that don't affect the database - as the only way it can reconstruct a field correctly is to have all the changes in the history, and you might need those options in some data migrations later on (for example, if you've set custom ...

Is Django migration necessary?

Migrations are not required. They can be useful for creating and tracking database changes via code, but Django applications will run properly without them.


2 Answers

You can squash it with the previous migration, sure.

Or if you don't want to output those migrations at all, you can override the makemigrations and migrate command by putting this in management/commands/makemigrations.py in your app:

from django.core.management.commands.makemigrations import Command from django.db import models  IGNORED_ATTRS = ['verbose_name', 'help_text', 'choices']  original_deconstruct = models.Field.deconstruct  def new_deconstruct(self):   name, path, args, kwargs = original_deconstruct(self)   for attr in IGNORED_ATTRS:     kwargs.pop(attr, None)   return name, path, args, kwargs  models.Field.deconstruct = new_deconstruct 
like image 74
Nick Retallack Avatar answered Oct 18 '22 23:10

Nick Retallack


This ticket addressed the problem.

If you have changed only help_text & django generates a new migration; then you can apply changes from latest migration to previous migration and delete the latest migration.

Just change the help_text in the previous migration to help_text present in latest migration and delete the latest migration file. Make sure to remove corresponding *.pyc file if it is present. Otherwise an exception will be raised.

like image 29
Pandikunta Anand Reddy Avatar answered Oct 18 '22 21:10

Pandikunta Anand Reddy