5

Django 2.0

I've created a model in Blog app,

class Category(models.Model):
    field1 = models....
    field2 = models....
    field3 = models....

and after some time I want to add a new field in that model.

class Category(models.Model):
    field1 = models....
    cover_pic = .models....
    field2 = models....
    field3 = models....

I followed this answer. But it gives me the following error.

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

Dead Pool
  • 133
  • 2
  • 9

3 Answers3

4

That answer is from 2014 and for an old Django version.

The workflow for adding new fields is simple:

  1. Write code for the field in your model.
  2. Run command manage.py makemigrations.
  3. Run command manage.py migrate.

This is all in the documentation.


Since, you already followed that answer and have run the manage.py --fake command, you have messed up your db state a little bit.

To recover, do this:

  1. Go to your "blog" app's "migration" folder.
  2. Look at the names of the migration files. They should look like 0001_category_field1.py, 0002_category_cover_pic.py.
  3. The faked migration will be the one with name something like 0002_category_cover_pic.py. Note its number, like 0002.
  4. Now you'll have to move back to the previously applied migration. So, if the faked migration number is 0002, the previously applied migration will be 0001.
  5. Now run this command - manage.py migrate --fake myapp 0001. Note the number of the migration file. You'll have to fake another migration back to the previously applied migration file.
  6. Now run command - manage.py migrate myapp.

This should fix your problem.

xyres
  • 20,487
  • 3
  • 56
  • 85
  • can you give me the link of documentation for this topic. I tried but couldn't find it – Dead Pool May 06 '18 at 06:53
  • 1
    BTW, my first try was that you mentioned in your answer. I simply added the field in model and ran the `makemigrations` still getting the same error. – Dead Pool May 06 '18 at 06:54
  • and yet the answer is old, but it also mentioned exactly the same steps as you mentioned except first two steps which have nothing to do there and has no effect. – Dead Pool May 06 '18 at 06:56
  • @DeadPool The [link to the documentation](https://docs.djangoproject.com/en/2.0/topics/migrations/#workflow). No, the answer is not same. It is about upgrading from an older django version to a newer django version, that's why the `--fake` parameter is used in the command. You also need to run `migrate` command to apply the newly generated migrations after `makemigrations`. – xyres May 06 '18 at 07:00
  • I'm getting the error in `makemigrations` phase, so it should be run well before running the `migrate` command. – Dead Pool May 06 '18 at 07:01
  • Well I have some unexpected result. When I am adding a new field in my `Post` model, it is running well but when I'm doing the same in `Category` model, it is showing the error above, both belongs to the same app `Blog` – Dead Pool May 06 '18 at 07:07
  • @DeadPool You might have messed up your migrations by running the `migrate --fake` command. This is also [documented](https://docs.djangoproject.com/en/2.0/ref/django-admin/#cmdoption-migrate-fake). Wait a few minutes, I'll try to find a solution. – xyres May 06 '18 at 07:14
  • Ok, yeah maybe right. I here, thanks for your effort. – Dead Pool May 06 '18 at 07:25
  • @DeadPool I've updated the answer with a recovery solution. See if it helps. – xyres May 06 '18 at 07:38
0

If you're problem is not solved yet and you don't have important data in your database, I would suggest start your database from fresh. Delete all your database tables, then delete all the migration files inside your app_name/migration folders. Now run the two commands and start developing.

python manage.py makemigrations
python manage.py migrate

Now you will have a fresh database and you are good to go. From next time try to follow the way mentioned in the above comment.

SK. Fazlee Rabby
  • 344
  • 4
  • 14
  • On the above question mentioned that the DB has been worked with three field and after a time with stored data on DB now we want add new column on it. So consider that we can't remove DB or old migrations file. – Benyamin Jafari Oct 14 '18 at 12:30
0

If you are adding a new field in the Django model and after deploying to any environment , you are getting error while accessing the field. Error: "1054, Unknown column"

Although i also was not able to figure out how to resolve it but i came up with a work around.

  1. I created a column manually in the DB .
  2. I added the same field in the model.
  3. Again tried to access the field and it worked like a charm.

I hope it helps your case.

NoobCoder
  • 1
  • 1