python manage.py migrate does not make any changes in the postgres database

蓝咒 提交于 2019-12-07 08:20:43

问题


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.


回答1:


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.




回答2:


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',
    }
}


来源:https://stackoverflow.com/questions/31715238/python-manage-py-migrate-does-not-make-any-changes-in-the-postgres-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!