Django South:从另一个表迁移 FK

发布于 2024-11-19 12:53:27 字数 582 浏览 6 评论 0原文

我正在做一些系统,我的表设计是这样的:

Table A:
   - ID
   - ForeignKey(C)

Table B:
   - ID
   - ForeignKey(A)

Table C:
   - ID

由于一些需求的变化,我需要将ForeignKey(C)从A移动到B,所以它是这样的:

Table A:
    - ID

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

-Table C:
    - ID

模式迁移很容易。但上次我在表 A 中已经有一些现有的 FK 数据。我不能立即删除它们。我需要将这个FK迁移到表B。B的结构不同,里面的数据也不同。当我将ForeignKey(C)添加到B的模型时,South要求我提供默认值,因为我没有提供任何默认值并且它不允许NULL。选择是退出 schemamigration 并手动添加默认值或提供 1 次值。

我的问题是,如何添加使用 South 从表 A 中的现有数据导入的默认/1 次值?我正在寻找这个类似的问题,但没有运气:(

谢谢。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-11-26 12:53:27

在进行这种类型的迁移时,我总是创建 3 个迁移:

  1. 创建新字段
  2. (数据迁移) 将数据从旧字段复制到新字段(可能通过转换)
  3. 删除旧字段

这样我就可以使用来自的自动模式迁移对于第一次和第三次向南迁移,只需要编写一些简单的转换代码即可。这与南方教程中的此示例类似。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文