迁移时Django FieldDoesNotExist异常

时间:2016-01-08 15:01:26

标签: python django sqlite migration

使用django 1.9。所以我试图迁移我的数据库但是我遇到了这个错误。我花了很多时间来解决这个问题并且没有成功。如果有必要,我可以上传更多代码。这是错误:

C:\Users\James\Desktop\James\Work\django\homepgcom>python manage.py migrate
Operations to perform:
  Apply all migrations: auth, interface, sessions, admin, contenttypes, userprofile
Running migrations:
  Rendering model states... DONE
  Applying interface.0002_auto_20160107_1635...Traceback (most recent call last):  
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\options.py", line 580, in get_field
return self.fields_map[field_name]
KeyError: None
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 "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 350, in execute_from_command_line
utility.execute()
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\core\management\__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\core\management\commands\migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\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 "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\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 "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\migrations\executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\migrations\migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\migrations\operations\fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\backends\base\schema.py", line 482, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\backends\sqlite3\schema.py", line 245, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\backends\sqlite3\schema.py", line 181, in _remake_table
self.create_model(temp_model)
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\site-packages\
django\db\backends\base\schema.py", line 250, in create_model
to_column = field.remote_field.model._meta.get_field(field.remote_field.field_name).column
File "C:\Users\James\AppData\Local\Programs\Python\Python35\lib\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: User has no field named None

非常感谢提前!

2 个答案:

答案 0 :(得分:2)

关于五分钟发布后,我想出了一个决议。以为我会分享它以防将来有人遇到这个问题。

  1. 删除所有应用的所有迁移
  2. 为所有应用
  3. 运行cookieName
  4. 然后迁移python manage.py makemigrations <appname>
  5. 然后一切都应该没问题

    感觉就像一个完全白痴花了这么多时间试图解决这个问题哦,好吧!

答案 1 :(得分:1)

对于任何刚接触Django的人来说,很容易发现迁移在团队合作中存在问题。因为失去了人们修改模型并进行迁移有人做错了导致问题。如果它和#39;在dev env中,删除迁移并重做初始步骤不是问题。

但如果它在生产环境中。您无法删除所有迁移。如果您这样做,您需要确保新数据库具有原始数据库的数据。这将花费大量时间而不是修复错误的迁移。

所以我想解决问题的正确方法是在运行时检查迁移文件手册

python manage.py migrate

如果发生错误,找到导致问题的字段,然后修改错误的迁移文件。

如果有

  

django.db.utils.OperationalError:(1050,&#34;表&#39; sometable&#39;   已存在

Django Table already exist会解决您的问题。

如果有

  

django.core.exceptions.FieldDoesNotExist:用户没有名为None的字段

这意味着你必须删除migrats.AddField或AlterFields。

operations = [
    migrations.AddField(
        model_name='user',
        name='user_current_plan_id',
        field=models.IntegerField(blank=True, null=True),
    ),
]

如果有

  

重复列名称

您可以按Duplicate column name

修复它

对我来说,一旦发生错误,不是问题,而是一系列问题......只是冷静下来,通过修改错误的迁移文件来修复它是一种比删除所有迁移并重新同步数据库数据更好的方法。 p>