django.db.migrations.RenameModel和AutoField序列名称 [英] django.db.migrations.RenameModel and AutoField sequence name
问题描述
我使用Django 1.8.7和PostgreSQL,并具有以下模型:
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')
我已经添加了RenameModel操作:
The I have added RenameModel operation:
migrations.RenameModel(
old_name='Permission',
new_name='TemplatePermission',
),
看起来一切正常,但是TemplatePermission.id字段的序列名称仍然是myapp_permission_id_seq:
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 |
...
有重命名序列的正确方法吗?这是Django中的错误吗(我发现了与Django 1.8.x和Oracle非常相似的错误报告和补丁)?
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)?
推荐答案
在我的情况下,我手动创建了sql迁移脚本.但是,如果您决定使用其他数据库,则可能无法正常工作.
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;'),
]
这篇关于django.db.migrations.RenameModel和AutoField序列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!