10

Using django 1.9. So I'm attempting to migrate my database however I am coming across this error. I have spent many hours trying to troubleshoot this and have had no success. I can upload more of my code if it is 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

Many thanks in advance!

ThankYOU
  • 317
  • 2
  • 8
  • how many apps you have? And here is exception `FieldDoesNotExist: User has no field named None` Can you write `User` model, if you change this. Or any code where you working with `User` – Ivan Semochkin Jan 08 '16 at 15:03
  • Thanks for the comment, but I've got a solution! – ThankYOU Jan 08 '16 at 15:15

2 Answers2

6

About five minutes after posting this I came up with a resolution. Thought I would share it in case anyone has this issue in the future.

  1. Delete all migrations for all your apps
  2. Run python manage.py makemigrations <appname> for all apps
  3. Then migrate python manage.py migrate

Then everything should be just fine

Feel like a total idiot to have spend so many hours trying to fix this, oh well!

ThankYOU
  • 317
  • 2
  • 8
  • 9
    mmm it might work for you, but you could not delete all migrations for your project. – BlaShadow Oct 17 '16 at 00:31
  • 7
    Very bad solution! If the migration scripts are already in prod, this will be bad or lead to further modification. – Dhia Jun 27 '17 at 12:54
  • 3
    I assume you are new to Django. There is a table in your database called `django_migrations` which keeps track of the migrations executed on your database. That's how django knows which migrations are new and need to be executed. If you remove your migrations and create new ones, it may not be an issue on your local machine cause you can easily wipe your database and create a new one. But if you have a running server with a live db, you need to fix `django_migration` table as well which is basically a bad practice. So long story short, find a fix instead of clearing migrations. – AliBZ Aug 22 '17 at 18:32
4

For anyone new to Django, It's easy to find that migration have wired problem in a teamwork env.cause Losts of people are modify the models and do the migrations Someone did it wrong and cause the problem.If it's in dev env, delete migrations and redo the initial step is not a problem.

but If it's in production env.You can't delete all the migrations.If you do that you need to ensure the new db have the data of the origin ones.That will take lot of time than fix the buggy migrations.

So I guess the correct way to fix the problem is check the migrations file manual when run

python manage.py migrate

if the error occurred, find the field or tables cause the problem ,then modify the wrong migration file.

If there a

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

Django Table already exist will fix your problem.

If there is a

django.core.exceptions.FieldDoesNotExist: User has no field named None

it's mean you have to delete the migrats.AddField or AlterFields.

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

if there is a

Duplicate column name

you can fix it by Duplicate column name

For me, once the error occurred , Not a problem but a series of questions。。just calm down, and fix it by modify the wrong migrations files is a better way than delete all migrations and resync the db data.