开发者

Django South: Migrating FK from another table

I was doing some system and my table design was like this:

Table A:
   - ID
   - ForeignKey(C)

Table B:
   - ID
   - ForeignKey(A)

Table C:
   - ID

Due to some requirement changes, I need to move ForeignKey(C) from A to B, so it goes like this:

Table A:
    - ID

Table B:
    - ID
    - ForeignKey(A)
    - ForeignKey(C)

-Table C:
    - ID

The schema migration was easy. But last time I already have some existing FK data in table A. Which I can't just delete right away. I need to migrate this FK's to table B. The structure of B is different as well as it's data inside. When I add ForeignKey(C) to B's model, South was asking me for a default value because I 开发者_JAVA百科didn't provide any default value and it is not allowing NULL. The choice was to quit schemamigration and add default value manually or provide 1-time value.

My question is, how can I add default/1-time value that is imported from existing data in table A using South?. I was searching for this similar problem but no luck :(

Thanks.


When doing this type of migration I always create 3 migrations:

  1. creates the new field
  2. (a datamigration) copies the data from the old field to the new field (possibly with a transformation)
  3. deletes the old field

This way I can use the auto schemamigration from south for the first and the third migration and only have to write some simple conversion code. This works like this example in the south tutorial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜