Dan*_*iel 2 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.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
Run Code Online (Sandbox Code Playgroud)
请注意,我已添加该字段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.
Run Code Online (Sandbox Code Playgroud)
好的,我做了一个./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:
Run Code Online (Sandbox Code Playgroud)
这里奇怪的是,这个模型还没有进入.那么,如果数据库中没有产品,为什么还需要提供默认值?
我开始拔头发了,昨天我尝试了几件事.3个小时后,我放弃了.
迁移系统的设计使得单个迁移可以应用于多个数据库.例如,您可以拥有开发版本,暂存版本以及一个或多个生产版本.这就是为什么制造的迁移是从一个独特的步应用的迁移,以及为什么makemgirations不能只看当前活动的数据库,以查看它没有任何行.如果您然后尝试将迁移应用于数据库,该怎么办?
您的案例中的解决方案很简单:由于没有行,因此选项1(在所有现有行上设置默认值)根本不会执行任何操作.因此,选择选项1,以及您喜欢的任何值.
| 归档时间: |
|
| 查看次数: |
2490 次 |
| 最近记录: |