What is the best approach to change primary keys in an existing Django app?

前端 未结 8 1636
野的像风
野的像风 2020-11-28 06:23

I have an application which is in BETA mode. The model of this app has some classes with an explicit primary_key. As a consequence Django use the fields and doesn\'t create

8条回答
  •  庸人自扰
    2020-11-28 07:23

    Currently you are failing because you are adding a pk column that breaks the NOT NULL and UNIQUE requirements.

    You should split the migration into several steps, separating schema migrations and data migrations:

    • add the new column, indexed but not primary key, with a default value (ddl migration)
    • migrate the data: fill the new column with the correct value (data migration)
    • mark the new column primary key, and remove the former pk column if it has become unnecessary (ddl migration)

提交回复
热议问题