我的模型中有这个字段created_at = models.DateTimeField( auto_now_add = True )
当我尝试进行迁移时出现错误:
You are trying to add the field 'created_at' with 'auto_now_add=True' to user wi
thout a default; the database needs something to populate existing rows.
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
我尝试设置默认值,但它说default
and auto_now_add
是互斥的。当然我可以只使用默认值而无需auto_now_add
但我想知道为什么会出现这个错误。我错过了什么?
您已经有一些行没有created_at
价值。
但是当您添加以下字段并运行迁移时
created_at = models.DateTimeField( auto_now_add = True)
它期望现有行有created_at
价值。因此,它会在迁移时向您发出警告。
为了解决这个问题。
您可以选择第一个选项,其中显示Provide a one-off default now (will be set on all existing rows)
。
通过选择第一个选项,您就要求获得价值。在这里您可以输入timezone.now()
。它将用当前时间戳填充现有行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)