[Django] #21638: Infinite migrations when using AUTH_USER_MODEL

39 views
Skip to first unread message

Django

unread,
Dec 18, 2013, 10:00:50 PM12/18/13
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
----------------------------+-----------------
Reporter: patrick@… | Owner:
Type: Bug | Status: new
Component: Migrations | Version:
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+-----------------
When using a custom class with AUTH_USER_MODEL, syncdb thinks that the
"username" field in that class has changed. Every time you run
{{{
python manage.py makemigrations
}}}
it generates a new migration file, even though that model has no changes.
If you run that migration, and call makemigrations again, it still thinks
the field has changed and will make even more migration files.

For example, my models.py:

{{{
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

class Person(AbstractUser):
favorite_color = models.CharField(max_length=64)

}}}

0001_initial.py created by "python manage.py makemigrations myapp"

{{{
# encoding: utf8
from django.db import models, migrations
import django.utils.timezone
import django.core.validators


class Migration(migrations.Migration):

dependencies = []

operations = [
migrations.CreateModel(
fields = [('id', models.AutoField(serialize=False,
primary_key=True, verbose_name='ID', auto_created=True),), ('password',
models.CharField(max_length=128, verbose_name='password'),),
('last_login', models.DateTimeField(default=django.utils.timezone.now,
verbose_name='last login'),), ('is_superuser',
models.BooleanField(default=False, verbose_name='superuser status',
help_text='Designates that this user has all permissions without
explicitly assigning them.'),), ('username',
models.CharField(max_length=30, help_text='Required. 30 characters or
fewer. Letters, digits and @/./+/-/_ only.', unique=True,
verbose_name='username',
validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a
valid username.', 'invalid')]),), ('first_name',
models.CharField(max_length=30, verbose_name='first name', blank=True),),
('last_name', models.CharField(max_length=30, verbose_name='last name',
blank=True),), ('email', models.EmailField(max_length=75,
verbose_name='email address', blank=True),), ('is_staff',
models.BooleanField(default=False, verbose_name='staff status',
help_text='Designates whether the user can log into this admin site.'),),
('is_active', models.BooleanField(default=True, verbose_name='active',
help_text='Designates whether this user should be treated as active.
Unselect this instead of deleting accounts.'),), ('date_joined',
models.DateTimeField(default=django.utils.timezone.now, verbose_name='date
joined'),), ('favorite_color', models.CharField(max_length=64),),
('groups', models.ManyToManyField(to='auth.Group', verbose_name='groups',
blank=True),), ('user_permissions',
models.ManyToManyField(to='auth.Permission', verbose_name='user
permissions', blank=True),)],
options = {'abstract': False, 'verbose_name': 'user',
'verbose_name_plural': 'users'},
bases = (models.Model,),
name = 'Person',
),
]
}}}
I'm suspicious of the "bases = (models.Model,)" instead of
"bases=(models.AbstractUser,)" but I don't know if that has any
significance.


And every time I run makemigrations after this, it creates a new migration
like the following (the field order changes, but the content is always the
same). If you run makemigrations ten times without changing your model,
you get ten new scripts that all look like this:
{{{
# encoding: utf8
from django.db import models, migrations
import django.core.validators


class Migration(migrations.Migration):

dependencies = [('myapp', '0001_initial')]

operations = [
migrations.AlterField(
name = 'username',
model_name = 'person',
field =
models.CharField(validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$',
'Enter a valid username.', 'invalid')], verbose_name='username',
max_length=30, help_text='Required. 30 characters or fewer. Letters,
digits and @/./+/-/_ only.', unique=True),
),
]
}}}

Discovered using Python 3.3.3, and Django 1.7 (alpha) commit
23d9f517dc3ca31816bb8596f5a59f1ae44304ee

Note: I double checked this occurred with a fresh database after reading
the warning
[https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a
-custom-user-model here], and confirmed that AUTH_USER_MODEL was set
before any tables were made.

--
Ticket URL: <https://code.djangoproject.com/ticket/21638>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 18, 2013, 10:24:01 PM12/18/13
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
----------------------------+--------------------------------------

Reporter: patrick@… | Owner:
Type: Bug | Status: new
Component: Migrations | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by jess@…):

* cc: jess@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:1>

Django

unread,
Dec 21, 2013, 7:28:17 PM12/21/13
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
----------------------------+------------------------------------

Reporter: patrick@… | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by timo):

* version: => master
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:2>

Django

unread,
Jan 12, 2014, 4:49:09 PM1/12/14
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
----------------------------+------------------------------------

Reporter: patrick@… | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------

Comment (by patrick@…):

In django/db/migrations/autodetector.py line 241, the old version of the
username field is compared against the current username field. Both the
old and new username fields have their own separate instance of a
django.core.validators.RegexValidator. The two RegexValidator objects have
the exact same values, but because they are separate instances and do not
have a comparison function they always compare as unequal.

Some possible solutions:

1) Ignore validators when comparing fields. My understanding is that the
validators only affect data and do not influence the database schema, but
I haven't fully explored their usage. I believe something similar was
proposed and turned down in ticket #21275 because the validators may still
be useful during the migration process.

2) Add an {{{__eq__}}} function to the RegexValidator class. This is quick
and easy, but the other validator classes may need this also to prevent
the same error in the future.

I'd be willing to contribute a patch, but I don't want to prescribe a
solution without feedback from maintainers who understand the overall
system better than I do.

--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:3>

Django

unread,
Jan 19, 2014, 1:57:12 PM1/19/14
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: patrick@… | Owner: Andrew
Type: Bug | Godwin <andrew@…>
Component: Migrations | Status: closed
Severity: Normal | Version: master
Keywords: | Resolution: fixed
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Godwin <andrew@…>):

* status: new => closed
* owner: => Andrew Godwin <andrew@…>
* resolution: => fixed


Comment:

In [changeset:"a68f32579145dfbd51d87e14f9b5e04c770f9081"]:
{{{
#!CommitTicketReference repository=""
revision="a68f32579145dfbd51d87e14f9b5e04c770f9081"
Fixed #21638: Validators are now comparable, stops infinite user mig'ns
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:4>

Django

unread,
Apr 15, 2014, 6:16:08 AM4/15/14
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: patrick@… | Owner: Andrew
Type: Bug | Godwin <andrew@…>
Component: Migrations | Status: new

Severity: Normal | Version: master
Keywords: | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by anonymous):

* status: closed => new
* resolution: fixed =>


Comment:

I am still encountering this issue on 1.7b1


{{{
$ git log
commit dda6224459ed843dfd63e85729613fcc60ce925f
Author: James Bennett <ja...@b-list.org>
Date: Thu Mar 20 19:42:11 2014 -0500

[1.7.x] Bump version numbers for 1.7 beta 1.
}}}

User Model
{{{
class User(AbstractUser):

def __unicode__(self):
return self.username
}}}

Running ./manage makemigrations
{{{
./manage.py makemigrations
Migrations for 'users':
0002_auto_20140415_1115.py:
- Alter field username on user
}}}

Results in
{{{
class Migration(migrations.Migration):

dependencies = [
('users', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(help_text=u'Required. 30 characters or
fewer. Letters, digits and @/./+/-/_ only.', unique=True, max_length=30,
verbose_name=u'username',
validators=[django.core.validators.RegexValidator(u'^[\\w.@+-]+$', u'Enter
a valid username.', u'invalid')]),
),
]
}}}

And this keeps happening over and over

--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:5>

Django

unread,
Apr 15, 2014, 6:18:49 AM4/15/14
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: patrick@… | Owner: Andrew
Type: Bug | Godwin <andrew@…>
Component: Migrations | Status: new
Severity: Normal | Version: master
Keywords: | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by wengole):

* cc: wengole (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:6>

Django

unread,
Apr 29, 2014, 7:36:53 PM4/29/14
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: patrick@… | Owner: Andrew
Type: Bug | Godwin <andrew@…>
Component: Migrations | Status: new
Severity: Normal | Version: master
Keywords: | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timo):

I cannot reproduce with the provided information due to `ValueError:
Lookup failed for model referenced by field polls.User.groups: auth.Group`
when running `makemigrations` a second time, which I believe is #22485. I
will leave this ticket open so we can check this after that is fixed.

--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:7>

Django

unread,
May 1, 2014, 7:05:03 AM5/1/14
to django-...@googlegroups.com
#21638: Infinite migrations when using AUTH_USER_MODEL
-------------------------------------+-------------------------------------
Reporter: patrick@… | Owner: Andrew
Type: Bug | Godwin <andrew@…>
Component: Migrations | Status: closed
Severity: Normal | Version: master
Keywords: | Resolution: fixed

Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* status: new => closed

* resolution: => fixed


Comment:

With the latest master and stable/1.7.x I cannot reproduce infinite
migrations given the above code.

--
Ticket URL: <https://code.djangoproject.com/ticket/21638#comment:8>

Reply all
Reply to author
Forward
0 new messages