5

This seems like a simple problem I am not sure what I am doing wrong. If for example I wanted to add a new field in one of my classes in models.py by changing:

class FeedBack(models.Model):
    feedback = models.CharField(max_length=600)
    user = models.ForeignKey(User,default="")

to

class FeedBack(models.Model):
    feedback = models.CharField(max_length=600)
    email = models.CharField(max_length=100)
    user = models.ForeignKey(User,default="")

then I run

python manage.py makemigrations

python manage.py migrate

and everything seems fine, but no changes have actually been made in the database. When trying to view the feedback table I receive the following exception:

column IngressoMonitor_feedback.email does not exist

also using psql \d+ on the table shows email has not been added

for now I could just use psql to add and alter tables, but I'd prefer to write it in models.py since that seems a lot easier to me.

Abendsen
  • 173
  • 2
  • 2
  • 5

2 Answers2

3

Make sure that the app containing that models.py file is included in INSTALLED_APPS of your project's settings file. In addition, please do not touch the files under the app's migration folder unless you are certain you know what you are doing. Please also make sure that the DB account specified in your settings file have the necessary privileges.

If you recently changed your Django version, this link might be of use to you. But give it a shot anyway and make the migrations per app in this case:

python manage.py makemigrations app_name

If all else fails, just drop the tables of the database, and regenerate everything from scratch. However, if at some point, you messed with any of the migration files, you might want to remove all of them before performing makemigrations to ensure that you have a new and working set of migration files that manage.py can work on.

Community
  • 1
  • 1
Wannabe Coder
  • 1,457
  • 12
  • 21
  • For this sole reason I hate Django. If I have to drop all tables, what are methods like `RenameField()` in migrations files for???? – WesternGun Nov 07 '17 at 14:40
  • I don't think you necessarily have to drop all the tables. In this scenario, I'd at least start by only dropping the affected ones. – Malik A. Rumi May 01 '18 at 17:16
0

You may need to reset your database user or owner password and try again. In mine I had to reset the default postgres user to not use any password from commandline using this link here.

Then tried running the command: python manage.py migrate and now works like charm.

Don't forget to also set your configurations from the django project: myprojectname/myprojectname/settings.py file. example here is mine;

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'polling_db',
        'USER': 'postgres',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
shiva
  • 5,083
  • 5
  • 23
  • 42
STREET MONEY
  • 534
  • 4
  • 8