迁移

2016-01-08 88 views
7

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

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 

非常感谢提前!

+0

你有多少个应用程序?这里是例外'FieldDoesNotExist:用户没有字段名称为None'如果你改变了这个,你能写'User'模型吗?或者你使用'User'的任何代码 –

+0

感谢您的评论,但我有一个解决方案! – ThankYOU

+0

为你感到高兴:) –

回答

2

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

  1. 删除所有迁移的所有应用
  2. 运行所有的应用程序
  3. 然后迁移python manage.py migrate

然后一切都应该就好

觉得自己像一个总白痴花了这么多小时试图解决这个问题,哦!

+4

嗯它可能适合你,但你不能删除你的项目的所有迁移。 – BlaShadow

+1

非常糟糕的解决方案!如果迁移脚本已经在制作中,这将会很糟糕或导致进一步的修改。 – DhiaTN

+1

我假设你是Django的新手。数据库中有一个名为'django_migrations'的表,用于跟踪在数据库上执行的迁移。这就是django如何知道哪些迁移是新的并需要执行的。如果您删除了迁移并创建了新迁移,那么可能不会成为本地计算机上的问题,因为您可以轻松擦除数据库并创建新数据库。但是如果你有一个正在运行的带有活动分贝的服务器,你还需要修复'django_migration'表,这基本上是一种不好的做法。长话短说,找到一个修复而不是清除迁移。 – AliBZ

1

对于任何新来Django的人来说,很容易发现迁移在团队合作环境中存在有线问题。人员流失是修改模型并进行迁移有人做错了并导致问题。如果它在dev env,删除迁移并重做第一步不是问题。

但是如果它在生产env.You不能删除所有的迁移。如果你这样做,你需要确保新的数据库有原始的数据。这将花费大量的时间比修复错误的迁移。

所以我想正确的方法来解决这个问题是检查迁移文件手动运行

python manage.py migrate

时,如果发生错误,找到字段或表造成的问题,然后修改了错误的迁移文件。

如果有一个

django.db.utils.OperationalError: (1050, "Table 'sometable' already exists

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

如果有

django.core.exceptions.FieldDoesNotExist: User has no field named 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

您可以通过Duplicate column name

我对其进行修复,一旦发生错误,不是一个问题,而是一个系列的questions..just冷静下来,并通过修改错误的迁移文件来修复它是比删除所有迁移和重新同步db数据更好的方法。