在 schemamigration 中,我确定不会有 null 值的 null=False 字段的默认值应该是什么?
我想向我的模型添加一个 ForeignKey
字段。为了实现这一目标,我执行了 3 个步骤:
- 将带有
null=True
的ForeignKey
字段添加到我的模型中,然后创建一个schemamigration
。 - 创建了一个
datamigration
,以便用合理的值填充该外键。应用此迁移后,我非常确定该字段中没有留下 NULL 值的对象。 - 将该
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.2
和 south-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:
- Added the
ForeignKey
field withnull=True
to my model, and then created aschemamigration
. - 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. - Changed that
ForeignKey
field tonull=False
, and then created anotherschemamigration
.
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
? (usingsouth
) - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是一个想法:由于这是一个外键,您可以将默认值设置为您知道永远不会有引用记录的记录,即 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.