Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django 1.7.1需要字段的默认值,但数据库中没有条目。为什么?_Python_Django_Django Migrations - Fatal编程技术网

Python Django 1.7.1需要字段的默认值,但数据库中没有条目。为什么?

Python Django 1.7.1需要字段的默认值,但数据库中没有条目。为什么?,python,django,django-migrations,Python,Django,Django Migrations,我遇到了一个奇怪的问题。我在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.M

我遇到了一个奇怪的问题。我在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\u monthly
字段。然后,我进行了一次
/manage.py迁移

(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个小时后,我放弃了。

迁移系统的设计目的是使单个迁移可以应用于多个数据库。例如,您可以有一个开发版本、一个临时版本和一个或多个生产版本。这就是为什么进行迁移与应用迁移是截然不同的一步,也就是为什么
makemgirations
不能只查看当前活动的数据库,以确定它没有任何行。然后,如果您尝试将迁移应用到执行迁移的数据库,该怎么办


本例中的解决方案很简单:因为没有行,所以选项1(在所有现有行上设置默认值)根本不起任何作用。因此,选择选项1和您喜欢的任何值。

您以前已经应用了迁移,因此表已经创建。现在添加的列不能为null,但尚未定义默认值

由于迁移将向现有表中添加一列,因此需要足够的数据,以便迁移不会违反模式;因此出现了提示


如果要删除该表,然后运行迁移,则不会遇到此错误。但是,由于初始迁移已经创建了表,因此任何未来的迁移都不能破坏其引用完整性。

我已经多次遇到这种情况,假设您的项目myproject中有名为app1的应用程序,在这种情况下,您可以做的最好的事情是删除app1/迁移,然后重试。它应该可以正常工作

嗨,凯文,谢谢您的解释。我理解你的意思,但在另外两个Django项目中,我可以做到这一点,而无需提供默认值。我仍然认为我的迁移出了问题。@Daniel:我无法与您的其他项目交流(也许您的字段有
null=True
?也许您在第一次运行
migrate
之前做了更改),但您在这里描述的正是应该发生的事情。一切正常,解决方案也很简单,因此无需拔头发。;-)谢谢各位。谢谢你的帮助。