Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - wont let me add a new field to my model because 1054, "Unknown column 'field list'")

Tags:

django

I am trying to simply add a boolean field to a model in my database following the simple rules:

- add field to model
- python manage.py makemigrations app
- python manage.py migrate app

Works all but 99% of the time. So during the second step (makemigrations), with my newly added field in my model raring to go, i get an error:

django.db.utils.OperationalError: (1054, "Unknown column 'model.field' in 'field list'")

Excellent. its not letting me make migrations by adding a new field..... because it cant find the field that I am trying to newly add... makes perfect sense!

Anyway, I have gone as far as deleting all my migrations, removing my new field, making migrations again, migrating... all fine - so now i have only 1 migration file (0001)...

Follow the same steps as above... ERROR

Am i missing something ridiculous here? I mean, adding a field to a model is very simple, and I have done it probably 1000 times. Why does Django tease me so

EDIT: Answer:

OK I have done it.

After deleting the migrations file, truncating the migrations table and migrating with 0001_initial.py, I made an empty migrations file (python manage.py makemigrations --empty app) and added the field in manually.... then I migrated and it worked! Baffled at this to be honest, but at least the change has been made:

  • Delete all migration files
  • Truncate the django_migrations table
  • comment the new boolean field
  • run python manage.py makemigrations
  • run python manage.py migrate --fake
  • run python manage.py makemigrations --empty app
  • add field in manually to the empty migrations file in the operations:

    migrations.AddField('modelName', 'fieldName', models.BooleanField(default=False)),
    
  • run python manage.py migrate

  • uncomment the new boolean field so it represents what you made in the migrations operations
like image 373
trouselife Avatar asked Oct 16 '22 14:10

trouselife


1 Answers

Disclaimer- Follow this only on local system, for production do understand the steps and then execute.

Kindly follow these steps:

  • Delete all migration files
  • Truncate the django_migrations table
  • comment the new boolean field
  • run python manage.py makemigrations
  • run python manage.py migrate --fake
  • Uncomment the boolean field
  • run python manage.py makemigrations
  • run python manage.py migrate

Generally these steps solve any kind of migration problem

An another reason can be if you are using django_rest_framework then the serialiser too needs to be updated as per your model change.

like image 124
argo Avatar answered Oct 20 '22 23:10

argo