Django 1.7 migration error

2019-03-04 12:45发布

问题:

I changed a field from CharField to ForeignKey on a Model called Availability, when I am trying to migrate I keep getting the error below:

ValueError: Lookup failed for model referenced by field reservation.Availability.location: useraccount.Location

Any idea why this could be happening?

Thanks

--------------UPDATED CODE--------------

App: reservation

from useraccount.models import Location

class Availability(models.Model):
    location = models.ForeignKey(Location)

App: useraccount

class Location(models.Model):
    town = models.CharField(max_length=100)
    county = models.CharField(max_length=100)

    def __str__(self):
        return self.town + ', ' + self.county

Stacktrace

System check identified some issues:

WARNINGS: notification.NoticeSetting.send: (1_6.W002) BooleanField does not have a default value. HINT: Django 1.6 changed the default value of BooleanField from False to None. See https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield for more information. Operations to perform: Apply all migrations: reservation Running migrations: Applying reservation.0010_auto_20141210_0357...Traceback (most recent call last): File "/Users/chirdeeptomar/envs/mydocbook/lib/python3.4/site-packages/django/apps/registry.py", line 148, in get_app_config return self.app_configs[app_label] KeyError: 'useraccount'

During handling of the above exception, another exception occurred:

回答1:

What follows is a hack which is so easy and so nasty to apply while developing or even better while learning other parts of django and you just need a quick solution to get the work done ... in other words, a sin ... like bitter chocolate.

First of all, I keep my database data in a json file in case I need to rebuild it:

./manage.py dumpdata --exclude auth.permission --exclude contenttypes  --exclude reversion --exclude admin.LogEntry --indent 2 > db.json

When I change something in my models and I see that migrations start throwing errors, I try deleting everything regarding the application under consideration from the database (or -even better- the whole database) and then I rebuild it:

$./manage.py migrate <myapp1> zero #No need for this if the whole database is destroyed
$rm -Rf <myapp(s/1)>/migrations/*
$./manage.py makemigrations <myapp1>( <myapp2> ... <myappN>)
$./manage.py migrate <myapp(s/1)>
$./manage.py loaddata ...

This is most of the times faster than debugging the error. Sometimes, however, causes more problems than it is supposed to solve. This is when the database json file comes handy.

A pittiful solution but this is my revenge when all those details make me very angry and I enjoy it.