Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Django migration alter field (AlterField) that is not touched?

When i do migration python manage.py makemigrations wall i see in console that Django (1.8.12) tells me a long list of fields that are touched:

Migrations for 'wall':
  0079_auto_20170302_0024.py:
    - Add field periodic_task_interval to userproject
    - Alter field bank_problems on bankireference
    - Alter field bank_problems_category on bankireference
    - Alter field bank_products on bankireference
    - Alter field bank_products_category on bankireference
    - Alter field extr_category on bankireference
    - Alter field extr_words on bankireference
    - Alter field neg_features on bankireference
    - Alter field neutral_features on bankireference
    - Alter field pos_features on bankireference
    - Alter field tonality_category on bankireference
    - Alter field tonality_words on bankireference
    - Alter field bank_problems on fbpagepost
    - Alter field bank_problems_category on fbpagepost
    - Alter field bank_products on fbpagepost
    - Alter field bank_products_category on fbpagepost
    - Alter field extr_category on fbpagepost

...... ad so on, near all fileds that is ManyToManyField. I also checked other migrations, looks like random choose of fields, because i see also tagulous.models.fields.TagField and even models.TextField.

BUT, i am sure a did not changed that fields, even touched them

This is what in 0079_auto_20170302_0024.py for untouched fields:

......
    migrations.AlterField(
        model_name='userproject',
        name='description',
        field=models.TextField(default='', verbose_name='Description', blank=True, null=True),
    ),
......

The only thing i have done is added - Add field periodic_task_interval to userproject so other fields - why they appear in migration? Can this effect a long time doing actual migration because of big database?

like image 607
Vic Nicethemer Avatar asked Mar 01 '17 21:03

Vic Nicethemer


People also ask

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

What is difference between migration and migrate in Django?

You should think of migrations as a version control system for your database schema. makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

How do you force migrations to a DB if some tables already exist in Django?

Move the already done migrations from that file into the empty migration you created.. then --fake that one. This will put Django's understanding of what the database looks like will be in sync with reality and you can then migrate as normal, applying the changes in the last migration file.

How does Django migration work?

Django keeps track of applied migrations in the Django migrations table. Django migrations consist of plain Python files containing a Migration class. Django knows which changes to perform from the operations list in the Migration classes. Django compares your models to a project state it builds from the migrations.


1 Answers

if you have choices= parametr in field, you should generate it in "stable way" (same order every time), for example with sorted()

TYPES = {
    'DISTRICT': 'Округ',
    'AREA': 'Район',
    'STATION': 'Станция метро',
}
type = models.CharField(
    max_length=255,
    choices=sorted(TYPES.items()),
    default='AREA',
)
like image 130
Ryabchenko Alexander Avatar answered Oct 26 '22 17:10

Ryabchenko Alexander