Add non-null and unique field with already populated model

邮差的信 提交于 2019-12-03 04:47:56

Unfortunatelly, I found no answer but I could create one solution:

  • First I have created a migration that allows the slug field to be nullable;
  • Then I have created another migration that populates the slug column with proper values to every row in the model;
  • Then another migration which adds the not-null constraint in the column.

You do your model changes (add field, change, etc), then you call manage.py makemigrations, then apply the migrations with manage.py migrate

You can add the field with null=True, then you e.g. make a script to populate it one time

Otherwise, if you need to populate the field within the migration you can write a custom one, see https://docs.djangoproject.com/en/1.7/ref/migration-operations/#writing-your-own

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