如何用南方重命名外键字段? [英] How to rename a foreignkey field with South?

查看:108
本文介绍了如何用南方重命名外键字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重命名简单的字符串等似乎很简单( Django - How使用South重命名模型字段



但是,当我尝试在ForeignKey字段上使用相同的时候,我会收到一个错误:

  _mysql_exceptions.OperationalError:(1091,Can not DROP[new_fkey_field_name]';检查列/键是否存在)

哪些源于尝试以某种原因运行向后的迁移?(跟踪中证明)



任何想法?

解决方案

更新:with $ $ c> mysql-5.5.30-1.fc18.x86_64

  MySQL-python = = 1.2.4 
Django == 1.4.2
南== 0.7.6

以下作品:

 类迁移(SchemaMigration_:
def forward(self,orm)):
db.rename_column('app_model ,'old_id','new_id')
db.alter_column('app_model','new_id',
self.gf('django.db.models.fields.related.ForeignKey')(
blank = True,
null = True,
to = orm ['app.OtherModel']
))

def backwards(self,orm)
db.rename_column('app_model','new_id','old_id')
db.alter_column('app_model','old_id',
self.gf('django.db.models .fields.related.ForeignKey')(
blank = True,
null = True,
to = orm ['app.OtherModel']
))

由于@Eloff的评论,South找不到原始的FK,原因不明,但似乎并不重要。不需要数据迁移(我相信),因为pk值不应该改变。



现场规范(使用 self.gf )取自南方自动生成的迁移,以实现一致性。 p>

Renaming a simple charfield etc seems easy (Django - How to rename a model field using South?)

However when I try using the same on a ForeignKey field I get an error:

_mysql_exceptions.OperationalError: (1091, "Can't DROP '[new_fkey_field_name]'; check that column/key exists")

Which stems from the migration trying to run the backwards for some reason (as evidenced in the trace).

Any ideas?

解决方案

Update: with mysql-5.5.30-1.fc18.x86_64 and

MySQL-python==1.2.4
Django==1.4.2
South==0.7.6

the following works:

class Migration(SchemaMigration_:
    def forwards(self, orm):
        db.rename_column('app_model', 'old_id', 'new_id')
        db.alter_column('app_model', 'new_id',
                        self.gf('django.db.models.fields.related.ForeignKey')(
                            blank=True,
                            null=True,
                            to=orm['app.OtherModel']
                        ))

    def backwards(self, orm):
        db.rename_column('app_model', 'new_id', 'old_id')
        db.alter_column('app_model', 'old_id',
                        self.gf('django.db.models.fields.related.ForeignKey')(
                            blank=True,
                            null=True,
                            to=orm['app.OtherModel']
                        ))

As @Eloff comments, South can't find the original FK for reasons unknown, but it doesn't seem to matter. There is no need for a data migration (I believe) as pk values should not change.

The field specification (using self.gf) is taken from South's auto-generated migrations for consistency.

这篇关于如何用南方重命名外键字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆