Django FieldDoesNotExist exception when migrating
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.
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.
- Delete all migrations for all your apps
- Run
python manage.py makemigrations <appname>
for all apps - 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!