6
votes

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.

2
I think this question is related to a South issue: south.aeracode.org/ticket/1179 and it's not resolved yet.L42y

2 Answers

12
votes

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.

0
votes

I solved this same problem the other day so I figured other people might find this useful. This happens when the migration is created before South had Django 1.5+ compatibility. I had a similar issue with using PybbM Forum App in my Django Site. The solution is to include your model and related models into the old migrations ORM. There are two ways to do this:

1) You can manually type out your model in the "models" list at the bottom of each failing migration file. 2) Use one of your existing passing migrations as a template and copy the desired orm models to the end.

Example: http://bpaste.net/show/Pv20CM5dTrbubzFZtiRY/

*Keep in mind that you will need to also copy in any related model your custom user might need so in the case of the custom user I made for you, you would need to copy in a 'ranks.rank', 'ranks.ranktest' and 'schools.school '