【问题标题】:Django/PostgreSQL manage.py flush error. look at output of 'django-admin sqlflush'Django/PostgreSQL manage.py 刷新错误。查看“django-admin sqlflush”的输出
【发布时间】:2018-12-06 16:25:54
【问题描述】:

构建:Heroku Python 服务器、Postgresql 10.4、Django 2、wagtail 2.1

我试图在 Heroku 上彻底销毁并重新创建我的应用程序数据库。以下是我遵循的步骤:

  1. 创建数据库转储(成功)
  2. rm 所有迁移并重新创建“初始”迁移(成功)
  3. 运行`heroku pg:reset DATABASE`(成功)
  4. 推送新的迁移和数据库转储(成功)
  5. 运行`heroku run python manage.py migrate`(成功)
  6. 运行`heroku run python manage.py flush`(**失败**)
JVsquad$ heroku run python manage.py flush
Running python manage.py flush on ⬢ my_app... up, run.2459 (Hobby)
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the 'my_app_db' database,
and return each table to an empty state.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: yes
CommandError: Database my_app_db couldn't be flushed. Possible 
reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin sqlflush'. That's the SQL this command wasn't able to run.

我的第 7 步将是 heroku run python manage.py loaddata db_dump.json,但它也失败了,因为刷新不起作用。

请帮忙

【问题讨论】:

  • django-admin sqlflush的输出是什么

标签: django postgresql heroku


【解决方案1】:

如果没有任何效果,您可以从 heroku GUI 中删除数据库并提供一个新的数据库。这将解决您当前的问题。

另外,这个帖子提出了一个解决方案

https://github.com/wagtail/wagtail/issues/1824

【讨论】:

  • 我看到了,但它是针对鹡鸰的过时版本
  • 我在想如果问题是one of the expected tables doesn't exist,我的迁移决定了哪些表是预期的..所以即使我安装了一个新的数据库,我也会遇到同样的问题。
  • 说得太早了,解决方案在那个线程中(github.com/wagtail/wagtail/issues/1824#issuecomment-182850722)。谢谢,奥哈斯羽衣甘蓝
【解决方案2】:

从 Mysql 迁移到 PostgresQL 后,我遇到了同样的问题。

TRUNCATE tablename CASCADE 的问题是我无法在 flush 方法中设置allow_cascade=True(在每个测试用例方法之后运行)

我覆盖了我的测试中使用的 Transaction 类:APITransactionTestCase

class PostgresTestCase(APITransactionTestCase):
"""
Override APITransactionTestCase class so the TRUNCATE table_name CASCADE is enabled

"""
def _fixture_teardown(self):

    # Allow TRUNCATE ... CASCADE and dont emit the post_migrate signal
    # when flushing only a subset of the apps
    for db_name in self._databases_names(include_mirrors=False):
        # Flush the database
        inhibit_post_migrate = (
                self.available_apps is not None or
                (  # Inhibit the post_migrate signal when using serialized
                    # rollback to avoid trying to recreate the serialized data.
                        self.serialized_rollback and
                        hasattr(connections[db_name], '_test_serialized_contents')
                )
        )
        call_command('flush', verbosity=3, interactive=False,
                     database=db_name, reset_sequences=False,
                     allow_cascade=True,
                     inhibit_post_migrate=True)

现在刷新也可以在 Postgres 中使用。

【讨论】:

    猜你喜欢
    • 2011-05-29
    • 2017-01-13
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多