sqlite3 manage.py migrate throws an error

TLDR, I’m trying to solve an issue of sqlite3 db not getting updated after adding a new filed to an existing model. Im using 2.2.24 version.

This is what I’ve done.

  1. I tried and added a new image field to an existing model:
    image = models.ImageField(upload_to='dialogs/', blank=True, null=True)
  2. I tried to make migrations with changes with python manage.py makemigrations which created a new .py file in migrations folder:
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=256)),
                ('text', models.TextField(blank=True, max_length=1000, null=True)),
                ('slug', models.SlugField(blank=True, null=True)),
                ('image', models.ImageField(blank=True, null=True, upload_to='dialogs/')),
  1. I tried to migrate python manage.py migrate, but I came accross an error
    django.db.utils.OperationalError: no such column: website_dialog.image

I guess the column website_dialog.image should be addedd automatically, right? It did work earlier like that.

I have tried:

  1. Removing the db.sqlite3 file cleaning the migrations folder of any .py files and trying to make a migration again, but then I was getting the error django.db.utils.OperationalError: no such table: website_homepage. Where homepage is my first model on the list.
  2. I tried to flush the db and then create super user again and make migrations but this was returning me a 500 error so I reverted changes.
  3. I also tried to add a website_dialog.image manually, but even though the field appeared in the admin panel, image could be loaded, but then couldn’t be removed.

What could be the problem that after adding the image field to the model, and creating the migration it doesn’t get migrated to the database?

Thanks in advanced

First, never delete a migration file unless you understand all of the implications of doing so.

Based upon what you describe here, what I would recommend:

  • Delete your database file
  • Delete all migration files
  • Rerun makemigrations and migrate

Thanks for answering.
I tried deleting the database file and all migration files, but when I try to run makemigrations, I get an error that there’s no table django.db.utils.OperationalError: no such table: website_homepage. Homepage is the first model in my models.py. The database file doesn’t get re-created.

Ok, you may need to do a migrate first for Django to initialize the database before your first makemigrations.