I just switched over to using a custom AUTH_USER_MODEL following this post.
It suggested the following forward migration:
def forwards(self, orm):
db.rename_table('auth_user', 'myapp_myuser')
db.rename_table('auth_user_groups', 'myapp_myuser_groups')
db.rename_table('auth_user_user_permissions',
'myapp_myuser_user_permissions')
if not db.dry_run:
# For permissions to work properly after migrating
orm['contenttypes.contenttype'].objects.filter(app_label='auth',
model='user').update(app_label='myapp', model='myuser')
This works great for existing databases for which syncdb
was executed while AUTH_USER_MODEL
was still the default auth.User
in settings.py. This is because the auth_user
table would've been created.
However, in a new installation using the new settings.py containing AUTH_USER_MODEL = 'myapp.MyUser'
, running syncdb
does NOT create the auth_user
table. Of course, if I now go and run the migrations for myapp using manage.py migrate myapp
, it gives the following error:
Running migrations for myapp:
- Migrating forwards to 0002_custom_auth_user_model.
> myapp:0001_initial
> myapp:0002_custom_auth_user_model
FATAL ERROR - The following SQL query failed: RENAME TABLE `auth_user` TO `myapp_myuser`;
The error was: (1017, "Can't find file: '.\\mydb\\auth_user.frm' (errno: 2)")
! Error found during real run of migration! Aborting.
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with: = RENAME TABLE `myapp_myuser` TO `auth_user`; []
= RENAME TABLE `myapp_myuser_groups` TO `auth_user_groups`; []
= RENAME TABLE `myapp_myuser_user_permissions` TO `auth_user_user_permissions`; []
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: myapp:0002_custom_auth_user_model
DatabaseError: (1017, "Can't find file: '.\\mydb\\auth_user.frm' (errno: 2)")
I thought of wrapping the migration in a conditional to check if the auth_user
table already exists, but South does not seem to support getting the database tables and I'm not sure this is the best idea either.
How can I fix the migration so it'll work with both new and existing installations?