2

In my django project I swapped the django auth user for a custom user model from the start. I use a proxymodel for the user as the AUTH_USER_MODEL. The project layout is somewhat like so,

manage.py
project/
    __init__.py
    settings.py
    urls.py
    wsgi.py
userlocal/
    models.py
userproxy/
    models.py

Where the relevant parts are as below,

project/settings.py

AUTH_USER_MODEL = 'userproxy.ProxyUser'

INSTALLED_APPS = (
    'south',

    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',

    'project',
    'userlocal',
    'userproxy',
)

userlocal/models.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin


class LocalUser(AbstractBaseUser, PermissionsMixin):
    """
    Custom User Model
    """

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    username = models.CharField(max_length=255, unique=True)
    email = models.CharField(max_length=255, unique=True)

    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_deleted = models.BooleanField(default=False)
    is_registered = models.BooleanField(default=True)

userproxy/models.py

from userlocal.models import LocalUser


class ProxyUser(LocalUser):

    class Meta():
        proxy = True

A syncdb works fine up to this point. Now I want to make changes to my userlocal model so I create an initial schemamigration.

python manage.py schememigration --initial userlocal

 + Added model userlocal.LocalUser
 + Added M2M table for groups on userlocal.LocalUser
 + Added M2M table for user_permissions on userlocal.LocalUser
Created 0001_initial.py ...

I add the following to my userlocal model,

first_name = models.CharField(max_length=255, null=True)
last_name = models.CharField(max_length=255, null=True)

And create a new migration for it,

python manage.py schemamigration --auto userlocal

 + Added field first_name on userlocal.LocalUser
 + Added field last_name on userlocal.LocalUser
Created 0002_auto__...py. ...

Now when I run a syncdb, this exists in an OperationalError

Syncing...
Creating tables ...
Creating table south_migrationhistory
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table userlocal_localuser
Traceback (most recent call last)
...
...
...
django.db.utils.OperationalError:
(1005, "Can't create table 'project_dev.#sql-11e1_10e' (errno: 150)")

When I debug the syncdb process the last query that is executed is,

Creating table userlocal_localuser
ALTER TABLE `django_admin_log`
ADD CONSTRAINT `user_id_refs_id_7b1a6083`
FOREIGN KEY (`user_id`)
REFERENCES `userlocal_localuser` (`id`);

So it attempts to create a constraint from django_admin_log to userlocal_localuser. But the latter is not created during the syncdb since it has migrations, right... ? So why is this constraint created... ?

sowa
  • 1,249
  • 16
  • 29

0 Answers0