Django模型无法迁移

时间:2016-04-23 10:46:50

标签: python django django-models

我有这样的模型文件:

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

在运行以下终端命令后添加第二个类GinLocal:

python manage.py makemigrations ginsdb
python manage.py migrate ginsdb

当我第二次添加第二个模型时它第一次运行正常我总是在migrate命令上遇到这个错误:

(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 

有没有人有想法?我没有看到问题。

编辑:

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()),
            ],
        ),
    ]

2 个答案:

答案 0 :(得分:2)

如错误日志中所述,问题位于:ginsdb.0002_ginlocal...中的../Path_To_Your_App/ginsdb/migrations

您有两个选择:

1-如果您的数据库为空且未输入任何内容,只需创建一个新数据库,在settings.py中更改数据库,清除../Path_To_Your_App/ginsdb/migrations中的所有迁移,然后尝试:< / p>

>>>python manage.py makemigrations
>>>python manage.py migrate ginsdb

2-如果您不想更改数据库应该找到错误原因,那么开始时应该发布../Path_To_Your_App/ginsdb/migrations/中的所有文件 并说出你设计模型的步骤。

在某些情况下,只有在您删除../Path_To_Your_App/ginsdb/migrations/ginsdb.0002_ginlocal...

时才会有效

答案 1 :(得分:0)

我建议您制作项目并复制应用程序以及sqlite文件,然后通过添加应用程序来更新settings.py。它将解决您的问题