pythondjangodjango-migrationsproxy-classes

Why does Django create migration files for proxy models?


I just created a proxy model and was surprised that manage.py makemigrations creates a new migration file with a migrations.CreateModel operation.

A proxy model does not create a new database table, it's just a different python interface to the same dataset and indeed manage.py sqlmigrate my_app_label 0042 returns nothing.

I thought that it might be used to create the proxy model ContentType but those are created on demand if they don't exist.

Is it used to trigger the creation of the proxy model permissions? There's a 6 year old open bug on proxy model permissions so I'm not really sure how this part is supposed to work now...

It used Django 1.8 to test this.

Edit: to clarify, Django creates a migration that does nothing for new proxy models so wouldn't we want Django to not create the migration in the first place if it's of no use?

Is there a use case where it would be useful to have the migration?


Solution

  • Ah, but if you open the migration in your editor, you will find that it's actually an empty migration! Here is an example

    class Migration(migrations.Migration):
        dependencies = [
            ('stackoverflow', '0009_auto_20160622_1507'),
        ]
    
        operations = [
            migrations.CreateModel(
                name='MyArticle',
                fields=[
                ],
                options={
                    'proxy': True,
                },
                bases=('stackoverflow.article',),
            ),
        ]
    

    And if you run ./manage.py sqlmigrate myapp 0010 (which is the number that corresponds to my migration above), what you get is what's on the next line (nothing).

    This is because the fields section of the migration is empty and option includes proxy = True. This setting prevents any SQL from being executed for this migration, and the original table is left untouched.

    So you may ask, why does Django bother to create an empty migration? That's because the proxy model may be referred to by another model in a future migration.