Inability to create superuser during syncdb when using GIS fields in AUTH_PROFILE_MODULE

Description

Overview

I have an AUTH_PROFILE_MODULE that contains a GIS PointField for maintaining a users current location. When a new user is created, I have a post_save signal handler that creates a UserProfile object for the new user. When I run syncdb, and try to create a superuser during the process, I get the following traceback:

This is due to the fact that the custom SQL commands have not been run yet. If I do a syncdb without creating the superuser in the process, and then create superusers from the command line after a successful syncdb, everything works correctly.

I am using PostgreSQL 8.4.3-1 and PostGIS 1.5.1-1.

Setup

The following assumes that you have a working GeoDjango installation, which I have. This was verified by running the GeoDjango tutorial without fail.

In settings.py add a profile model

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

In the application accounts, add the following simple UserProfile model

I believe the reason the problem happens is that custom SQL is done after the prompt for creating a super user, which means that the geometry column in UserProfile has not been created yet. This is why if I create super users after syncdb has properly run, everything works correctly. I assigned this to the component GIS, but I believe it could be more related to the order in which things happen during syncdb.

I'll accept the ticket for now, but I'm not sure if there will be a patch acceptable for this edge-case scenario that has an easy work-around (run syncdb first, and create the superuser afterwards):

$ ./manage.py syncdb --noinput
$ ./manage.py createsuperuser

From my brief review, the only way to fix this would be to change when the post_syncdb is emitted, as it is done before the index creation SQL (which is now where the AddGeometryColumn SQL is actually generated). The side-effects of this 'fix' could be far-reaching have many backwards-incompatible consequences, so for now I would recommend the workaround given above.