django南迁,不设默认 [英] django south migration, doesnt set default
问题描述
created_at = models.DateTimeField(default = datetime.now)
tag_id = models.PositiveIntegerField(default = 0)
南方将这两个字段添加到数据库,但无法设置其默认值要手动完成。
这个错误有补丁吗?
更新
我已经尝试使用 auto_now_add = True
设置默认日期,但这也不设置默认值。在字段中添加 null = True
在南方生成的迁移脚本中添加了一个 db.alter_column
。但是,只有删除 NOT NULL
约束,不添加默认值。
在南方或其他地方,这不是一个错误。
我认为你对Django的默认值通常是如何工作感到困惑的。 Django不会在数据库模式中设置默认值。当创建新的实例时,它将直接应用于Python。您可以通过执行 manage.py sqlall
并查看生成的SQL不包含默认值
属性来进行验证。 p>
I use south to migrate my django models. There is however a nasty bug in south. It doesn't set default values in Postgres Databases. Example:
created_at = models.DateTimeField(default = datetime.now)
tag_id = models.PositiveIntegerField(default = 0)
South will add these 2 fields to database, but fail to set their default values, which needs to be done manually.
Is there any patch for this bug?
UPDATE
I had already tried setting default date with auto_now_add=True
, but that is also not setting defaults. Adding null=True
in field adds a db.alter_column
in migration script produced by south. But that only removes NOT NULL
constraint, doesnt add a default. Same for integer field
This is not a bug, in South or elsewhere.
I think you are confused about how default values work in Django generally. Django does not set default values in the database schema. It applies them directly in Python, when a new instance is created. You can verify this by doing manage.py sqlall
and see that the generated SQL does not contain default
attributes.
这篇关于django南迁,不设默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!