使用 Django 的新迁移框架,假设我在数据库中已经存在以下模型:
class TestModel(models.Model):
field_1 = models.CharField(max_length=20)
我现在想在模型中添加一个新的 TextField,它看起来像这样:
class TestModel(models.Model):
field_1 = models.CharField(max_length=20)
field_2 = models.TextField(blank=True)
当我尝试使用 迁移此模型python manage.py makemigrations
时,我收到以下提示:
You are trying to add a non-nullable field 'field_2' to testmodel 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)
2) Quit, and let me add a default in models.py
null=True
我可以通过添加to轻松解决此问题field_2
,但 Django 的约定是避免在基于字符串的字段上使用 null,例如 CharField 和 TextField(来自https://docs.djangoproject.com/en/dev/ref/models/fields/)。这是一个错误,还是我误解了文档?