Opened 7 years ago
Closed 7 years ago
#29521 closed Bug (invalid)
Infinite migrations without changes on code when Choices are store in a dictionary
Reported by: | Roberto Fernandez Diaz | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 2.0 |
Severity: | Normal | Keywords: | makemigrations migration choices distionary |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have this code on my model
class Alcance(models.Model): # CHOICES CONCEJO = 'CO' PROVINCIA = 'PR' PAIS = 'PA' ZONA_AFECTADA_OPTIONS = { (CONCEJO, 'concejo'), (PROVINCIA, 'provincia'), (PAIS, 'pais') } # DATABASE FIELDS ... zona_afectada = models.CharField(max_length=2, choices=ZONA_AFECTADA_OPTIONS, default='CO')
The problem is that each time I execute the command, manage.py makemigrations
It always detect a change on this part of the code, generating always a new migration file
Example :
accounts\migrations\0004_auto_20180610_1356.py
# Generated by Django 2.0 on 2018-06-10 13:56 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('accounts', '0003_auto_20180602_2019'), ] operations = [ migrations.AlterField( model_name='alcance', name='zona_afectada', field=models.CharField(choices=[('PA', 'pais'), ('PR', 'provincia'), ('CO', 'concejo')], default='CO', max_length=2), ), ]
accounts\migrations\0005_auto_20180624_0126.py
# Generated by Django 2.0 on 2018-06-23 23:26 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('accounts', '0004_auto_20180610_1356'), ] operations = [ migrations.AlterField( model_name='alcance', name='zona_afectada', field=models.CharField(choices=[('CO', 'concejo'), ('PR', 'provincia'), ('PA', 'pais')], default='CO', max_length=2), ), ]
accounts\migrations\0006_auto_20180624_1205.py
# Generated by Django 2.0 on 2018-06-24 10:05 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('accounts', '0005_auto_20180624_0126'), ] operations = [ migrations.AlterField( model_name='alcance', name='zona_afectada', field=models.CharField(choices=[('PA', 'pais'), ('CO', 'concejo'), ('PR', 'provincia')], default='CO', max_length=2), ), ]
And so on...
As you can see the problem is due to the order in which the choices appear, that seems to be order randomly.
This is done without me changing the order of the elements of the dictionary or anything on the code that I show on the top.
First of all, you are using
set
, notdict
. And order of elements in both of them are not fixed. So I believe this is not a problem with Django, store your choices in list or tuple as explained in documentation.