Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.migrations.RenameModel and AutoField sequence name

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

class Permission(models.Model):

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

The I have added RenameModel operation:

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

Looks that all work OK but sequence name for TemplatePermission.id field 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    | 

...

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

like image 762
crazyh Avatar asked Dec 29 '15 17:12

crazyh


1 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;'),
]
like image 185
gawi Avatar answered Sep 20 '22 17:09

gawi