【发布时间】:2019-09-05 07:53:58
【问题描述】:
我在模型中添加了一个新字段,但之后我删除了 db.sqlite3(以确保我不会在下面出现错误)
agrawalo@:~/myapp> ls
README.md config core manage.py requirements.txt
但我在运行 makemigrations 时仍然收到此错误
agrawalo@:~/myapp> ./manage.py makemigrations
You are trying to add a non-nullable field 'high52' to stock without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
class Stock(models.Model):
name = models.CharField(max_length=255)
code = models.CharField(max_length=20, db_index=True)
price = models.DecimalField(max_digits=19, decimal_places=2)
diff = models.DecimalField(max_digits=19, decimal_places=2)
open_price = models.DecimalField(max_digits=19, decimal_places=2)
previous_close = models.DecimalField(max_digits=19, decimal_places=2)
low52 = models.DecimalField(max_digits=19, decimal_places=2)
high52 = models.DecimalField(max_digits=19, decimal_places=2)
last_updated = models.DateTimeField()
objects = DataFrameManager()
def save(self, *args, **kwargs):
''' On save, update timestamps '''
self.last_updated = timezone.now()
return super(Stock, self).save(*args, **kwargs)
def __str__(self):
return str(self.code)
low52 和 high52 是新添加的字段。请注意,其他现有字段都不会引发此错误。
【问题讨论】:
-
您是否在模型中添加了任何新字段?
-
我在模型中添加了一个新字段,但之后我删除了 db.sqlite3(以确保我不会在下面出现错误)
-
不是删除数据库。这是关于迁移的。您需要为新添加的字段添加默认值或将该字段设为可选。根据您的用例做出决定。
-
但是没有现有记录,那它为什么要抱怨。如何使字段可选?
-
在您的问题本身中添加新添加字段的代码。
标签: django django-models