django.db.migrations.RenameModel and AutoField sequence name

Question:

I use Django 1.8.7 and PostgreSQL and have the following model:

class Permission(models.Model):

    name = models.CharField(max_length=255)
    template = models.ForeignKey(Template, related_name='permissions')

Then I have added RenameModel operation:

    migrations.RenameModel(
        old_name='Permission',
        new_name='TemplatePermission',
    ),

It looks that everything works OK, but the sequence name for TemplatePermission.id field is still myapp_permission_id_seq:

postgres=# d+ myapp_templatepermission
                                            Table "public.myapp_templatepermission"
   Column    |          Type          |                          Modifiers                           | Storage  | Description 
-------------+------------------------+--------------------------------------------------------------+----------+-------------
 id          | integer                | not null default nextval('myapp_permission_id_seq'::regclass) | plain    | 

...

Is there a right way to rename sequence? Is it a bug in Django (I have found very similar bug report and patch for Django 1.8.x and Oracle)?

Asked By: crazyh

||

Answers:

In my case I manually created sql migration script. However, this might not work if you decide to use different db.

operations = [
  migrations.RenameModel(
    old_name='Permission',
    new_name='TemplatePermission',
  ),
  migrations.RunSQL('alter sequence myapp_permission_id_seq rename to myapp_templatepermission_id_seq;'),
]
Answered By: gawi