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:
这似乎是一个简单的问题,我不知道我做错了什么。例如,我想在模型的一个类中添加一个新字段。py通过改变:
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管理。py makemigrations
python manage.py migrate
python管理。py迁移
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
列IngressoMonitor_feedback。电子邮件不存在
also using psql \d+ on the table shows email has not been added
此外,在表中使用psql \d+还显示没有添加电子邮件
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.
现在,我可以使用psql来添加和修改表,但是我更喜欢在模型中编写它。这对我来说似乎容易多了。
1 个解决方案
#1
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.
确保包含该模型的应用程序。py文件包含在项目设置文件的INSTALLED_APPS中。此外,请不要触摸应用程序迁移文件夹下的文件,除非您确定自己知道正在做什么。还请确保设置文件中指定的DB帐户具有必要的特权。
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:
如果您最近更改了Django版本,这个链接可能对您有用。但是不管怎么说,在这个案例中,每个应用程序都要进行迁移:
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.
如果所有这些都失败了,只需删除数据库中的表,然后从头重新生成所有内容。但是,如果在某个时候,您弄乱了任何迁移文件,您可能希望在执行标记迁移之前删除所有的迁移文件,以确保您拥有一个新的、可工作的迁移文件集。py可以工作。
#1
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.
确保包含该模型的应用程序。py文件包含在项目设置文件的INSTALLED_APPS中。此外,请不要触摸应用程序迁移文件夹下的文件,除非您确定自己知道正在做什么。还请确保设置文件中指定的DB帐户具有必要的特权。
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:
如果您最近更改了Django版本,这个链接可能对您有用。但是不管怎么说,在这个案例中,每个应用程序都要进行迁移:
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.
如果所有这些都失败了,只需删除数据库中的表,然后从头重新生成所有内容。但是,如果在某个时候,您弄乱了任何迁移文件,您可能希望在执行标记迁移之前删除所有的迁移文件,以确保您拥有一个新的、可工作的迁移文件集。py可以工作。