如何使用SQLAlchemy / Python创建Postgres数据库并避免会话使用错误

时间:2019-06-30 02:56:33

标签: python postgresql sqlalchemy flask-sqlalchemy

我正在尝试在Python中创建一个postgresql数据库。我可以创建数据库,但不能关闭数据库会话(或SQLAlchemy会话?)。除非重新启动应用程序,否则以后将无法删除它。我收到的错误是稍后尝试删除它时:

Traceback (most recent call last):
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
    cursor, statement, parameters, context
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.ObjectInUse: database "db_ff264a9c_b693_457b_9c51_928786e93577" is being accessed by other users
DETAIL:  There is 1 other session using the database.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/trial.py", line 147, in <module>
    db_helper.remove_master_database(db, engine, custom_id)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/helpers/db_helper.py", line 194, in remove_master_database
    execute_in_session(db.session, remove_db_func)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/helpers/db_helper.py", line 266, in execute_in_session
    raise e
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/helpers/db_helper.py", line 257, in execute_in_session
    result = a_function(session)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/helpers/db_helper.py", line 217, in _remove_master_database
    db_api.drop_database(engine, a_db.db_name)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/mara_app/db_api.py", line 383, in drop_database
    execute_with_autocommit(engine, query)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/mara_app/db_api.py", line 394, in execute_with_autocommit
    conn.execute(query)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 982, in execute
    return self._execute_text(object_, multiparams, params)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1155, in _execute_text
    parameters,
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    e, statement, parameters, cursor, context
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1466, in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 383, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 128, in reraise
    raise value.with_traceback(tb)
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
    cursor, statement, parameters, context
  File "/Users/rabraham/Documents/dev/fifteenrock/mara/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (psycopg2.errors.ObjectInUse) database "db_ff264a9c_b693_457b_9c51_928786e93577" is being accessed by other users
DETAIL:  There is 1 other session using the database.

[SQL: drop database db_ff264a9c_b693_457b_9c51_928786e93577;]
(Background on this error at: http://sqlalche.me/e/e3q8)

Process finished with exit code 1

到目前为止,我尝试了不同的代码,但是它们都导致了相同的问题。我也尝试过NullPool。即

我当前的是: 我的引擎是在其他地方使用以下代码创建的:

from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(db_string) # or engine = create_engine(db_string, poolclass=NullPool)
# Not sure about db_session is required but putting it on SO
db_session = scoped_session(sessionmaker(autocommit=False,
                                     autoflush=False,
                                     bind=engine))

我的创建数据库代码:

def create_database(engine, database_name: str):
    query = f"create database {database_name};"
    conn = engine.connect()
    conn.execution_options(isolation_level="AUTOCOMMIT").execute(query)
    conn.close()
    engine.dispose()

0 个答案:

没有答案