tices
tices

Reputation: 96

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

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

Upvotes: 0

Views: 130

Answers (2)

ionut_tolci
ionut_tolci

Reputation: 26

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.

Upvotes: 1

K.D
K.D

Reputation: 132

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

Upvotes: 0

Related Questions