Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't perform data migrations using django 1.5 Custom user class

I have many applications with historic south initial migrations, which i wanted to convert to django 1.5 applicable. So i swapped out all orm['auth.User'] references in migration files with custom ones, but when i try to run those migrations i get following error:

Error in migration: django_notify:0001_initial KeyError: "The model 'customuser' from the app 'profiles' is not available in this migration."

The migration in question is this: http://bpaste.net/show/2CwaYrlNifNTd5gcHUfK/

My custom User class is:

class CustomUser(AbstractUser):
    image = models.ImageField(_('Image Field'), upload_to='user_images')

I am also unable to convert my'profiles' app to south using convert_to_south command. I get the following error:

Creating init.py in '/Users/tejinder/Projects/basidia/apps/profiles/migrations'...

  • Added model profiles.CustomUser

    • Added M2M table for groups on profiles.CustomUser

    • Added M2M table for user_permissions on profiles.CustomUser

Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate profiles

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'profiles.CustomUser' which has not been installed or is abstract.

What could have been gone wrong? Thanks in advance.

like image 571
tejinderss Avatar asked Feb 01 '13 11:02

tejinderss


1 Answers

See this answer: Migrating existing auth.User data to new Django 1.5 custom user model?

For others that may have a similar problem starting with a custom user model:

If you are using 'django.contrib.auth' and have a custom user model you can't run syncdb without having your custom user model includded in the installed apps. You will get this error

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'myapp.User' which has not been installed or is abstract. admin.logentry: 'user' has a relation with model myapp.User, which has either not been installed or is abstract.

So to fix that you need to include your app containing your user model in the installed apps and now when you run syncdb it will add all the tables for your own models. So you have to convert your app to south since the tables have already been created.

python manage.py syncdb
python manage.py migrate
python manage.py convert_to_south myapp

This will create 0001_initial and you get this error:

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'myapp.User' which has not been installed or is abstract.

Workaround:

python manage.py syncdb
python manage.py migrate
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake

You will still get the error above when you run convert_to_south but you can ignore it for now. The South documentation says:

convert_to_south: South will automatically make and pretend to apply your first migration

I think the problem is that the model validation is causing convert_to_south to error out before it pretends (--fake) to apply your first migration.

So the workaround is to basically do the fake migration that got skipped.

like image 136
Dan G Avatar answered Sep 22 '22 22:09

Dan G