permission name not updating after migration

I have a custom User model and custom permissions. After a while, I found that there was a typo in the name of a permission, so I decided to update it in the code that creates it, then I ran makemigrations and migrate. The problem is that name did not change in the DB even though the migration was applied.

What is more interesting is that when I deleted my database and run migrations from the start, the names got updated even though the permission name in the initial migration was different.

EXAMPLE:
The initial setup

CUSTOM_PERMISSIONS = (
    ('some_codename', 'some human readable description'),
    ('some_codename2', 'some human readable description with RORRE'),
)

class User(AbstractUser):
    # custom logic
    class Meta:
        ordering = ('-pk',)
        verbose_name = _('user')
        verbose_name_plural = _('users')
        default_permissions = tuple()
        permissions = CUSTOM_PERMISSIONS

and the initial migration has…

options={
                'verbose_name': 'user',
                'verbose_name_plural': 'users',
                'ordering': ('-pk',),
                'permissions': (('some_codename', 'some human readable description'), ('some_codename2', 'some human readable description with RORRE')),
                'default_permissions': (),
            },

…in the migration…

later I update the names of all custom permissions

CUSTOM_PERMISSIONS = (
    ('some_codename', 'some NEW human readable description'),
    ('some_codename2', 'some human readable description without ERROR'),
)

and run makemigrations which gives me a migration with:

operations = [
        migrations.AlterModelOptions(
            name='user',
            options={'default_permissions': (), 'ordering': ('-pk',), 'permissions': (('some_codename', 'some NEW human readable description'), ('some_codename2', 'some human readable description without ERROR')), 'verbose_name': 'user', 'verbose_name_plural': 'users'},
        ),
    ]

and I run migrate, but the description is the same as in the initial migration. I then rename the db.sqlite file and do a new migrate call and the custom permissions have the updated values.

EDIT:
It is good to note that this is the same with a different db (i.e. Postgres)


Is there a way to update custom permission names without making a custom migration to update the permissions table?

Couple different thoughts -

  1. I think this does not do what you think it’s going to do. See the docs for AlterModelOptions.

  2. There’s a big difference between changing the name of a permission and changing the description. (The combination of name and app are unique, and form the natural key, the description is just descriptive text.)

  3. I do not see any specific function in the migration operations for making changes to database table contents, only it’s structure. You could write a special operation using either the RunSQL or RunPython function to make these types of changes.

@KenWhitesell and that is what I did. I figured that it was the issue…

So besides the automatic migration the only choice is to make a custom migration. Topic solved then

Well, no, I wouldn’t agree with the use of the word ‘only’.
You could always write a custom-admin command if you wanted to do something outside the normal chain of migration operations, or just write an SQL script to run directly on the database.