私は奇妙な問題に遭遇しました。Mac OS X Yosemite で Django 1.7.1 を使用しており、ローカルの MySQL データベースを構成しています。
通常、私はモデルを作成し、別のフィールドを追加したい場合は./manage.py migrate
、Django が新しいデータベース列を作成するだけです。
私はまさにそれをしました。これが私のモデルです:
from django.db import models
from django.utils.translation import ugettext as _
class Product(models.Model):
CATEGORY_CHOICES = (
('apphosting', _('Application Hosting')),
('webhosting', _('Web Hosting'))
)
category = models.CharField(max_length=25, choices=CATEGORY_CHOICES)
name = models.CharField(max_length=25)
price_monthly = models.FloatField()
def __unicode__(self):
return self.name
フィールドを追加したことに注意してくださいprice_monthly
。次に、次のことを行いました./manage.py migrate
。
(mysphere)dmanser@ragamuffin:~/git/mysphere on master [!?]$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: crispy_forms
Apply all migrations: customers, sessions, admin, sites, flatpages, contenttypes, products, auth
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
わかりました、私は を実行します./manage.py makemigrations
。結果は次のようになります。
(mysphere)dmanser@ragamuffin:~/git/mysphere on master [!?]$ ./manage.py makemigrations
You are trying to add a non-nullable field 'price_monthly' to product 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
Select an option:
ここで奇妙なことに、このモデルにはまだエントリーがありません。では、データベースに製品がないのに、なぜデフォルト値を提供する必要があるのでしょうか?
私は髪を引っ張り始めており、昨日いくつかのことを試しました. 3時間後、あきらめました。