Django 1.7.1需要一个字段的默认值,但数据库中没有条目。为什么? [英] Django 1.7.1 requires a Default value for field - but no entry is in database. Why?

查看:148
本文介绍了Django 1.7.1需要一个字段的默认值,但数据库中没有条目。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题。我在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 
要执行的操作:
同步未迁移的应用程序:crispy_forms
应用所有迁移:客户端,会话,管理员,网站,平板,内容类型,产品,auth
同步应用程序而不进行迁移:
创建表...
安装自定义SQL ...
安装索引。
运行迁移:
没有要应用的迁移。
您的模型具有尚未反映在迁移中的更改,因此不会应用。
运行'manage.py makemigrations'进行新的迁移,然后重新运行'manage.py migrate'以应用它们。

好的,我做一个 ./ manage.py makemigrations ,这导致:

 (mysphere)dmanser @ ragamuffin:〜/ git / mysphere on master [! ] $ ./manage.py makemigrations 
您正在尝试向产品添加不可为空的字段price_monthly,而不使用默认值;我们不能这样做(数据库需要填充现有行的东西)。
请选择一个修复:
1)现在提供一次性默认(将在所有现有行上设置)
2)退出,并让我在models.py $中添加一个默认值b $ b选择一个选项:

这里奇怪的是,这个模型还没有进入。那么为什么我需要提供一个默认值,如果没有数据库中的产品?



我开始拉我的头发,我昨天尝试了几件事情。 3小时后,我放弃了。

解决方案

迁移系统的设计使得单个迁移可以应用于多个数据库。例如,您可以拥有开发版本,分期版本和一个或多个生产版本。这就是为什么使迁移是从应用迁移的一个明显的步骤,为什么 makemgirations 不能只看当前活动的数据库看到它没有任何行。如果您尝试将迁移应用于数据库,那该怎么办?



您的案例中的解决方案很简单:由于没有行,选项1(设置默认值在所有现有行上)根本不会做任何事情。所以选择选项1,以及您喜欢的任何值。


I ran into a strange problem. I'm using Django 1.7.1 on Mac OS X Yosemite and I have configured a local MySQL database.

Normally, I create a model and if I want to add another field, I just do a ./manage.py migrate and Django creates a new database column.

I did exactly that. Here's my model:

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

Please note that I have added the field price_monthly. Then, I did a ./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.

So ok, I do a ./manage.py makemigrations, which results in:

(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:

The strange thing here is, that this model has no entry yet. So why should I need to provide a default value if there's no product in the database?

I'm starting to pull my hair out and I have tried several things yesterday. After 3 hours, I gave up.

解决方案

The migrations system is designed so that a single migration can be applied to more than one database. For example, you could have a development version, a staging version, and one or more production versions. That's why making the migration is a distinct step from applying the migration, and why makemgirations can't just look at the currently active database to see that it doesn't have any rows. What if you then try to apply the migration to a database that does?

The solution in your case is simple: since there are no rows, option 1 (setting a default on all existing rows) won't do anything at all. So choose option 1, and any value you like.

这篇关于Django 1.7.1需要一个字段的默认值,但数据库中没有条目。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆