Why does Django create migration files for proxy models?
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.
I believe migrations
are generated because the databases is affected and migrations
are how Django signals database changes. The structure is not changed, but entries are added in (at least) two tables:
- A new
ContentType
is added todjango_content_type
for the proxy model. - Permissions specific to the Proxy Model are added to
auth_permission
. I assume this "always" happens unless the proxy uses the exact same class name. It certainly does happen -- we actually use a proxy model to access User using different permissions without touching the default User model.
Both of these details are actually noted in the comment chain of the issue linked by the OP (e.g. comment #31) because they contribute to the bug (i.e. that Django looks for permissions in a different app than those actually generated in auth_permissions
).