Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django South Migration does not work the first time around

I'm running a South migration python manage.py syncdb; python manage.py migrate --all which breaks when run on a fresh database. However, if you run it twice, it goes through fine! On the first try, I get

DoesNotExist: ContentType matching query does not exist. Lookup parameters were {'model': 'mymodel', 'app_label': 'myapp'}

After failure, I go into the database select * from django_content_type but sure enough it has

13 | my model     | myapp      | mymodel  

Then I run the migration python manage.py syncdb; python manage.py migrate --all and it works!

So how did I manage to make a migration that only works the second time around? By the way this is a data migration which puts the proper groups into the admin app. The following method within the migration is breaking it:

@staticmethod
def create_admin_group(orm, model_name, group_name):
    model_type = orm['contenttypes.ContentType'].objects.get(app_label='myapp', model=model_name.lower())
    permissions = orm['auth.Permission'].objects.filter(content_type=model_type)
    group = orm['auth.Group']()
    group.name = group_name
    group.save()
    group.permissions = permissions
    group.save()

(The migration files come from an existing working project which means a long time ago I had already run schemamigration --initial. I'm merely trying to replicate the database schema and initial data onto a new database.)

like image 910
Mark Avatar asked Dec 30 '25 17:12

Mark


2 Answers

Turns out this is a bug in South.

http://south.aeracode.org/ticket/1281

like image 95
Mark Avatar answered Jan 02 '26 09:01

Mark


Of course its going to be like this, you have not made any intial schemamigrations. The right way would be like this:

  1. Register your django apps with south first. So something like:
    python manage.py schemamigration --initial <app_name>.
  2. Then you run manage.py syncdb.
  3. After this, you run migrate like so python manage.py migrate <apps>, please note that simply running migrate will just migrate all your registered apps. I tend to do this.
  4. If you change models to change the schema, then you can simply use:
    manage.py schemamigration --auto

The problem that you are alluding to is this. Once you run syncdb, you already get a table crated, south had nothing to do with this. What you are hence doing is querying a database that has no migration control (iirc).

like image 23
Games Brainiac Avatar answered Jan 02 '26 09:01

Games Brainiac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!