django南迁,不设默认 [英] django south migration, doesnt set default

查看:101
本文介绍了django南迁,不设默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用南来迁移我的django模型。南方有一个讨厌的bug。它不会在Postgres数据库中设置默认值。示例:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆