python manage.py migration不会在postgres数据库中进行任何更改 [英] python manage.py migrate does not make any changes in the postgres database

查看:104
本文介绍了python manage.py migration不会在postgres数据库中进行任何更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题,我不确定自己在做什么错. 例如,如果我想通过以下方式在models.py中的一个类中添加一个新字段:

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

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

然后我跑步

python manage.py makemigrations

python manage.py migration

一切似乎都很好,但是实际上数据库中没有进行任何更改.尝试查看反馈表时,出现以下异常:

列IngressoMonitor_feedback.email不存在

还在表上使用psql \ d +表示电子邮件尚未添加

现在我可以只使用psql来添加和更改表,但是我更喜欢将它写在models.py中,因为这对我来说似乎要容易得多.

解决方案

确保包含该models.py文件的应用程序包含在项目的设置文件的INSTALLED_APPS中.此外,除非确定您知道自己在做什么,否则请不要触摸该应用程序的迁移文件夹下的文件.还请确保在您的设置文件中指定的数据库帐户具有必要的特权.

如果您最近更改了Django版本,则此链接对你有用.但是无论如何,还是尝试一下吧,在这种情况下,可以按应用进行迁移:

python manage.py makemigrations app_name

如果其他所有方法均失败,则只需删除数据库表,然后从头开始重新生成所有内容.但是,如果在某个时候弄乱了任何迁移文件,则可能需要在执行makemigrations之前将所有迁移文件删除,以确保您可以使用manage.py可以处理的新迁移文件集.

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.

解决方案

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.

这篇关于python manage.py migration不会在postgres数据库中进行任何更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆