Django FieldDoesNotExist exception on migration

Using django 1.9. Therefore, I am trying to migrate my database, but I am encountering this error. I spent many hours eliminating this and was not successful. I can upload more of my code if necessary. Here is the error:

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 

Thank you very much in advance!

+7
python django migration sqlite3
source share
2 answers

About five minutes after posting this question, I came up with a solution. Thought I'd share it if someone had such a problem in the future.

  • Delete all migrations for all your applications.
  • Run python manage.py makemigrations <appname> for all applications
  • Then do the python manage.py migrate migration

Then everything should be just fine

Feel the complete idiot spend so many hours trying to fix it, oh well!

+2
source share

For those new to Django, it's easy to find that migration is related to a wired issue in teamwork. env.cause Losing people changes models and performs migration. Someone did it wrong and caused a problem. If it is in dev env, removing migrations and repeating the initial step is not a problem.

but if it happens during the production process. You cannot delete all migrations. If you do this, you need to ensure that the new db contains the source data. It will take a lot of time than to fix errors with errors.

So, I think the correct way to fix the problem is to check the migration file manual at startup

python manage.py migrate

if an error occurs, locate the field or tables causing the problem, and then modify the invalid migration file.

If there

django.db.utils.OperationalError: (1050, "The table" sometable "already exists

Django table already exists , fix your problem.

If there

django.core.exceptions.FieldDoesNotExist: user does not have a field named None

That means you need to remove migrats.AddField or AlterFields.

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

if exists

Duplicate Column Name

you can fix it duplicate column name

For me, as soon as an error occurred, not a problem, but a series of questions. Just take it easy and fix it by changing the wrong migration files, this is a better way than deleting all migrations and re-synchronizing the db data.

+1
source share

All Articles