<p>我遇到了一个奇怪的问题。我在macosxyosemite上使用django1.7.1,并配置了一个本地MySQL数据库。在</p>
<p>通常,我创建一个模型,如果我想添加另一个字段,我只需执行一个<code>./manage.py migrate</code>,Django将创建一个新的数据库列。在</p>
<p>我就是这么做的。这是我的模型:</p>
<pre><code>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
</code></pre>
<p>请注意,我已经添加了字段<code>price_monthly</code>。然后,我做了一个<code>./manage.py migrate</code>:</p>
^{pr2}$
<p>好吧,我做一个<code>./manage.py makemigrations</code>,结果是:</p>
<pre><code>(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:
</code></pre>
<p>奇怪的是,这个模型还没有进入。那么,如果数据库中没有产品,为什么需要提供默认值?在</p>
<p>我开始拔头发了,昨天我试了好几样东西。三个小时后,我放弃了。在</p>