Reputation: 1575
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.
Upvotes: 2
Views: 2996
Reputation: 681
I have come across this many times, suppose you have app called app1 inside your project myproject, best thing you can do in this situation is to go and delete app1/migrations then try again it should work fine
Upvotes: 0
Reputation: 174624
You have already applied migrations before, so the table has already been created. Now you are adding a column that cannot be null, but have not defined a default value.
Since migrations will add a column to an existing table, it needs enough data so that your migration does not violate the schema; hence the prompt.
If you were to delete the table, and then run the migration you would not face this error. However as the initial migration has already created the table, any future migrations cannot not violate its referential integrity.
Upvotes: 1
Reputation: 48922
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.
Upvotes: 7