0

I'm creating a functionality that allows an admin to drop a database from the Django admin. However, I keep getting errors that a drop database cannot be execute from a transaction block or that the database is open.

I have tried solutions placed here and here, either writing it in a template and executing from SQLAlchemy directly but no luck.

My current code block is this

    engine = sa.create_engine('postgresql_string')
    session = sessionmaker(bind=engine)()
    session.close_all()
    engine.dispose()
    session.autocommit = True
    session.connection().connection.set_isolation_level(0)
    session.execute('DROP DATABASE IF EXISTS test_db;')
    session.connection().connection.set_isolation_level(1)

But it still will throw an error even when I terminated all activity from pg_stat_activity, any other ideas on how to implement this?

Community
  • 1
  • 1
Minh
  • 2,180
  • 5
  • 23
  • 50
  • Are you trying to drop the whole database or a single table? – arlyon Jan 05 '17 at 00:21
  • Where does this code sit in the project? It's a difficult thing to help with because a database connection could come from anywhere. – arlyon Jan 05 '17 at 07:45

0 Answers0