Opened 8 years ago

Closed 8 years ago

#26737 closed Bug (invalid)

models.EmailField(unique=True) in custom AbstractBaseUser throws auth.E003 during makemigrations

Reported by: Matthew Miller Owned by: nobody
Component: Core (Management commands) Version: 1.9
Severity: Normal Keywords:
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'm trying to implement a custom AbstractBaseUser to replace the default User class. For the most part I'm following the implementation detailed here, with some additional fields and nothing for REQUIRED_FIELDS:

class Account(AbstractBaseUser):
    email = models.EmailField(unique=True, db_index=True)

    # ...snip...

    USERNAME_FIELD = 'email'

Unfortunately when I run manage.py makemigrations I see the following error:

pbapi.Account: (auth.E003) 'Account.email' must be unique because it is named as the 'USERNAME_FIELD'.

I can suppress this error with the following:

SILENCED_SYSTEM_CHECKS = ['auth.E003']

This allows makemigrations to proceed, but I get the following output for the email field:

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Account',
            fields=[
                # ...snip...
                ('email', models.EmailField(max_length=254)),

When I modify the above migration output by hand to include unique=True:

 fields=[
    # ...snip...
    ('email', models.EmailField(max_length=254, unique=True)),

The next time I run makemigrations Django creates a migration that removes that constraint:

operations = [
    migrations.AlterField(
        model_name='account',
        name='email',
        field=models.EmailField(max_length=254),
    ),
]

I'm not really sure what the culprit is. I've used this exact custom class before in earlier versions of Django without issue (the latest Django I can remember trying this in was 1.8), but for some reason Django 1.9 hates this class. Am I doing something wrong?

Change History (2)

comment:1 by Matthew Miller, 8 years ago

I'm embarrassed to say this but the cause was strictly PEBKAC. I'd accidentally redefined the email field further down in the model as just a plain EmailField. Removing that fixed the issue.

comment:2 by Matthew Miller, 8 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top