How do I add another field to my model without getting an error?

Question:

I added another field to my Listing() model called highest_bid. However, when I try to look at the listing model in the /admin page I get the OperationalError:
no such column: auctions_listing.highest_bid

After adding this field I tried using makemigrations, but it said that there were no changes and I also tried using migrate but that also said that there was nothing to migrate.

I also tried removing null=True on the field but that did not change anything.

How do I add this field without getting an error?

models.py:

class Listing(models.Model):
    ...
    highest_bid = models.FloatField()
    # highest_bid = models.FloatField(null=True)
    # they both returned the same error
Asked By: tices

||

Answers:

how makemigrations didn’t add the changes, you have to find a way to implement make migrations and create a file in the migrations folder otherwise, your field will not be in the DB and you will keep seeing the same error
so try ./manage.py makemigrations your_app_name

Answered By: K.D

According to this answer you have to make sure you:

  1. Add your app to INSTALLED_APPS inside settings.py, otherwise makemigrations will not check for changes in your models
  2. Run ./manage.py makemigrations <app_name> if your app doesn’t have a migrations module yet (i.e. it’s going to be your app’s 0001_initial.py migration)

If you can cross these two requirements off your list (i.e. you’re doing subsequent changes to your models), then ./manage.py makemigrations without any <app_name> should simply work.

Answered By: ionut_tolci
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.