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
に追加することでこれを簡単に修正できますfield_2
が、Django の規則では、CharField や TextField などの文字列ベースのフィールドで null を使用しないようにしています ( https://docs.djangoproject.com/en/dev/ref/models/fields/から)。これはバグですか、それともドキュメントを誤解していますか?