Reputation: 8118
I'm having a models file like this:
from django.db import models
class Gin(models.Model):
objectId = models.CharField(max_length=100, unique=True)
alcohol = models.FloatField(null=True, blank=True, default=None)
name = models.CharField(max_length=100)
picture = models.CharField(max_length=200)
class GinLocal(models.Model):
objectId = models.CharField(max_length=100)
origin = models.TextField()
serve = models.TextField()
aroma = models.TextField()
The second class GinLocal is added after I run the following terminal commands:
python manage.py makemigrations ginsdb
python manage.py migrate ginsdb
The first time it runned fine the second time when I added the second model I always get this error on the migrate command:
(myvenv) ➜ gins4udjango python manage.py migrate ginsdb
Operations to perform:
Apply all migrations: ginsdb
Running migrations:
Rendering model states... DONE
Applying ginsdb.0002_ginlocal...Traceback (most recent call last):
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/options.py", line 580, in get_field
return self.fields_map[field_name]
KeyError: <django.db.models.fields.CharField>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 235, in create_model
definition, extra_params = self.column_sql(model, field)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 134, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 967, in db_parameters
return {"type": self.db_type(connection), "check": []}
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 958, in db_type
rel_field = self.target_field
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 861, in target_field
return self.foreign_related_fields[0]
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 594, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 581, in related_fields
self._related_fields = self.resolve_related_fields()
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 574, in resolve_related_fields
else self.remote_field.model._meta.get_field(to_field_name))
File "/Users/donpironet/Documents/Development/Private/gins4udjango/myvenv/lib/python3.5/site-packages/django/db/models/options.py", line 582, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Gin has no field named <django.db.models.fields.CharField>
(myvenv) ➜ gins4udjango
Does anyone have an idea? I don't see the problem.
EDIT:
Latest migration file
# -*- coding: utf-8 -*-
# Generated by Django 1.9.5 on 2016-04-23 10:45
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ginsdb', '0007_delete_ginlocal'),
]
operations = [
migrations.CreateModel(
name='GinLocal',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('objectId', models.CharField(max_length=100)),
('origin', models.TextField()),
('serve', models.TextField()),
('aroma', models.TextField()),
],
),
]
Upvotes: 1
Views: 14097
Reputation: 1309
This is what caused this problem for me.
I added a form, which needed some initialization from a model, so, following an example from stack overflow, I did something like this:
class QRCodeForm(forms.Form):
queryset = Mixer.objects.all().order_by('mixer_name')
...
The problem with this is that it accesses the database before the migration can be run, so it doesn't work.
Because the query is at the class level, it is run when the class is LOADED, not when it's instantiated.
So, when you start Django, the main file imports urls.py, which imports views, which imports forms.py, which then runs the query. The migration hasn't run, so the database state doesn't match the code, so you get an error.
The solution is to move the initialization into _init_(). I believe you will need to assign the final form field back to a class variable, at the end of _init_()
QRCodeForm.mixers = forms.MultipleChoiceField(widget=forms.SelectMultiple(, choices=options)
Upvotes: 0
Reputation: 395
I will suggest you to make the project and copy the app and as well as the sqlite file then update you settings.py by adding your app. It will solve your problem
Upvotes: -1
Reputation: 438
As mentioned in error log, problem is in : ginsdb.0002_ginlocal...
that is located in ../Path_To_Your_App/ginsdb/migrations
You have 2 choices:
1- If your database is empty and you have not entered anything in it can simply create a new database, change you database in settings.py
, clear all migrations in ../Path_To_Your_App/ginsdb/migrations
and then try this:
>>>python manage.py makemigrations
>>>python manage.py migrate ginsdb
2- If you don't prefer to change your database should find cause of error, for beginning should post all files in your ../Path_To_Your_App/ginsdb/migrations/
and say the steps you go to design your models.
In some cases it will simply work when you delete ../Path_To_Your_App/ginsdb/migrations/ginsdb.0002_ginlocal...
Upvotes: 3