如何在 Heroku 上完全重置 Postgres 数据库?

How to completely reset Postgres database on Heroku?

我在 Heroku 上部署了一个学习项目。它配置了一个 Postgres 数据库。我对我的 Django 项目的模型进行了一些重大更改,销毁了旧数据库并配置了一个新数据库,它完全是空的,但它不像空数据库那样工作。

当我 运行 命令 heroku run python manage.py makemigrations 时,我收到错误消息

You are trying to add a non-nullable field....

为什么我在销毁旧数据库后收到此消息?

首先,你永远不应该在 Heroku 上 运行 manage.py makemigrations

当您的代码到达那里时,应该没有模型更改来生成新的迁移。 运行 makemigrations 本地 创建迁移文件。 运行 migrate 在本地和 Heroku 上将迁移应用到您的数据库。

现在已经不存在了,这可能是由现有迁移 文件 引起的,而不是数据库中的任何内容。如果你真的想重新开始,你可以从你的每个应用程序的 migrations/ 目录中删除文件。

最后,无需销毁和重新配置您的数据库来重置它。相反,您可以 use heroku pg:reset:

The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.