在 schemamigration 中,我确定不会有 null 值的 null=False 字段的默认值应该是什么?

发布于 2024-11-15 23:43:28 字数 1670 浏览 3 评论 0原文

我想向我的模型添加一个 ForeignKey 字段。为了实现这一目标,我执行了 3 个步骤:

  1. 将带有 null=TrueForeignKey 字段添加到我的模型中,然后创建一个 schemamigration
  2. 创建了一个datamigration,以便用合理的值填充该外键。应用此迁移后,我非常确定该字段中没有留下 NULL 值的对象。
  3. 将该 ForeignKey 字段更改为 null=False,然后创建另一个 schemamigration

但是,我在步骤 3 中遇到了问题:

? The field 'MyModel.fkfield' does not have a default specified, yet is NOT NULL.
? Since you are making this field non-nullable, you MUST specify a default
? value to use for existing rows. Would you like to:
?  1. Quit now, and add a default to the field in models.py
?  2. Specify a one-off value to use for existing columns now
? Please select a choice:

嗯...这个字段没有默认值有意义,而且现在我很确定没有对象将该字段设置为 NULL。所以... 我不知道如何正确回答这个问题。如果可以的话,我会说“不要设置任何默认值,如果你发现其中有任何 NULL 则抛出错误柱子”。

目前,作为解决该问题的一种解决方法,我选择选项 2,然后使用 1 作为一次性默认值。这是一个非常糟糕的解决方案,但至少 south 不会抱怨它并创建迁移。

  • 将字段更改为 NOT NULL 的正确方法是什么? (使用 south
  • south 要求我为不应有的字段提供默认值时,我该怎么办?

我正在使用 django-1.2south-0.7


相关问题: Django South - 将 a null=True 字段转换为 null=False 字段

相关文档:部分3:高级命令和数据迁移

(脚注:在 StackOverflow,我们有两个对我来说似乎相同的标签:“django-south”和“south”,但我没有足够的声誉来建议同义词)

I want to add a ForeignKey field to my model. In order to achieve that, I did 3 steps:

  1. Added the ForeignKey field with null=True to my model, and then created a schemamigration.
  2. Created a datamigration in order to fill that foreign key with a sensible value. After this migration is applied, I'm pretty sure there is no object left with a NULL value at that field.
  3. Changed that ForeignKey field to null=False, and then created another schemamigration.

However, I'm having problems at the step 3:

? The field 'MyModel.fkfield' does not have a default specified, yet is NOT NULL.
? Since you are making this field non-nullable, you MUST specify a default
? value to use for existing rows. Would you like to:
?  1. Quit now, and add a default to the field in models.py
?  2. Specify a one-off value to use for existing columns now
? Please select a choice:

Well... No default value makes sense for this field, and also by now I'm pretty sure no object has that field as NULL. So... I don't know how to answer this question correctly. If I could, I would say "don't set any default value, and throw an error if you find any NULL in that column".

For now, as a workaround just to get over that question, I'm choosing option 2 and then using 1 as the one-off default value. This is a quite bad solution, but at least south doesn't complain about it and creates the migration.

  • What is the correct way of changing a field into NOT NULL? (using south)
  • What should I do when south asks me for a default value for a field that shouldn't have one?

I'm using django-1.2 and south-0.7.


Related question: Django South - turning a null=True field into a null=False field

Related documentation: Part 3: Advanced Commands and Data Migrations

(footnote: here at StackOverflow we have two tags that seem equal for me: "django-south" and "south", but I don't have enough reputation to suggest synonyms)

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

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

发布评论

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

评论(1

阿楠 2024-11-22 23:43:28

只是一个想法:由于这是一个外键,您可以将默认值设置为您知道永远不会有引用记录的记录,即 0 或 -1。如果 South 实际上尝试使用此默认值,您的数据库将引发错误。

Just a thought: Since this is a foreign key, you can set the default value to a record you know will never have a referenced record, i.e. 0 or -1. Your db will raise an error if south will actually try to use this default.

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