简体   繁体   中英

Django/PostgreSQL manage.py flush error. look at output of 'django-admin sqlflush'

Build: Heroku Python server, Postgresql 10.4, Django 2, wagtail 2.1

I'm trying to essentially destroy and recreate my app DB on Heroku. Here are the steps I've followed:

  1. create db dump (success)
  2. rm all migrations and recreated the 'initial' migrations (success)
  3. run `heroku pg:reset DATABASE` (success)
  4. push new migrations and db dump (success)
  5. run `heroku run python manage.py migrate` (success)
  6. run `heroku run python manage.py flush` (**failed**)
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.

My 7th step was going to be heroku run python manage.py loaddata db_dump.json but it also failed, because the flush won't work.

HELP PLEASE

If nothing works, you can delete the database form heroku GUI and provision a fresh database. this would solve your immediate problem.

Also, this thread suggests a solution

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

I had the same issue after moving from Mysql to PostgresQL.

The issue with TRUNCATE tablename CASCADE was that I couldn't set the allow_cascade=True in the flush method (that is run after each test case method)

I override the Transaction class used in my tests: 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)

Now the flush works in Postgres too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM