No Such Column Error in Django App After South Migration

你离开我真会死。 提交于 2019-11-28 03:24:36

Probably easiest way for you will be to start migrations from scratch.

Delete all migrations/* files for the app which you try to fix. Restore your models.py to the state which is at the moment on the database (by the help of version control tools, or just comment out the new fields). Then initialize migrations:

manage.py migrate my_app --delete-ghost-migrations
manage.py schemamigration my_app --init
manage.py migrate my_app --fake

This will create a record in migrations of what current database structure looks like.

Now add your changes to models.py and south will now what has changed:

manage.py schemamigration my_app --auto
manage.py migrate my_app

Something else to keep an eye out for: you will often get this error (DatabaseError: no such column: appname_model.fieldname) if you are using a default value in a ForeignKey relation, and you add a field to that FK model.

Something like (in your models.py):

class MyAppModel(models.Model):
    my_foreign_key = models.ForeignKey(FkModel,
                                       default=lambda: FkModel.objects.get(id=1),
                                       null=True)

Then in a new migration, you add a new field to your FkModel:

class FkModel(models.Model):
    new_field = models.IntegerField('New Field Name', blank=True, null=True)

You will get an error when running a South schemamigration:

DatabaseError: no such column: myappmodel_fkmodel.new_field

You can solve this by making sure your initial South migration includes this default value lambda function, but then remove the default value in the next migration.

This has bitten me in the past. Hopefully it will help someone in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!