[Django]-Django mssql setup : error initializing DB

1👍

Rather than tinker with the other migration files, make migration 008 into a two part exercise, first operation to just take the constraint off and change the length, then a second operation to put the unique constraint back (so that the username field is still unique as intended) and tweak the other parameters as designed.

Fortuitously (or cleverly) the migration code does the constraint thing before trying to change the length so the first operation succeeds despite the unique constraint already being True from the field creation. Should that order of execution change then we might need 3 operations – first take the constraint off, second change the length, third put the constraint back.

Having fixed the file, run

manage.py migrate

again.

I have tried just finishing off a failed migrate by running it again, and also deleting the database completely then creating it again empty and running migrate again to build from scratch. Both seem to work without error and leave you with the intended constraints and other field attributes in place.

To apply this fix, just modify the operations list in Migration in your local version of:

… Lib\site-packages\django\contrib\auth\migrations\008_alter_user_username_max_length.py

to do the two actions, thus:

operations = [
    migrations.AlterField(
        model_name='user',
        name='username',
        field=models.CharField(
            max_length=150,
            unique=False,
        ),
    ),
    migrations.AlterField(
        model_name='user',
        name='username',
        field=models.CharField(
            error_messages={'unique': 'A user with that username already exists.'},
            help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.',
            max_length=150,
            unique=True,
            validators=[validators.UnicodeUsernameValidator() if six.PY3 else validators.ASCIIUsernameValidator()],
            verbose_name='username',
        ),
    ),
]
👤Rick

0👍

i have just managed to solve this, well for my case anyway

using the above information, i altered the file 008_alter_user_username_max_length (in my windows version it is in “C:\Python34\Lib\site-packages\django\contrib\auth\migrations”

line 23 is :

unique=True

i changed this to :

unique=False

i redid

python manage.py migrate

and success

👤adrien

-1👍

i have exactly the same issue, i tried the “weird workaround of the day” but made no difference at all. i am using django 1.8, with django-mssql version 1.8. my database is MS SQL 2012 express.

i also tried using the different providers, however only the SQLOLEDB would connect in my case.

i ran the below command, and got the below result, highlighing the files causing the issue

C:\djangocode\testsite01>python manage.py showmigrations --list
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [ ] 0008_alter_user_username_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [ ] 0001_initial

i am using Python 3.4

👤adrien

Leave a comment