Выполните файлы DDL / SQL с помощью SqlAlchemy и получите хорошие сообщения об ошибках - PullRequest
0 голосов
/ 03 мая 2018

Рассмотрим простой файл SQL, подобный этому:

dummy.sql:

DROP TABLE IF EXISTS blah;

CREATE TABLE blah AS
SELECT * 
FROM blahblah2;

Я могу запустить его из командной строки и получить хорошее сообщение об ошибке:

$ mysql -u ... -h ... ... < /tmp/dummy.sql 
ERROR 1146 (42S02) at line 3: Table 'yourdb.blahblah2' doesn't exist

Или я могу запустить его из SQL Alchemy так:

import sqlalchemy

ddl_sql = open("/tmp/dummy.sql").read()

connect_string = "mysql+mysqldb://.../"
engine = sqlalchemy.create_engine(connect_string)

engine.execute(ddl_sql)

Когда я запускаю его из SQL Alchemy, я получаю гигантскую трассировку стека со следующей основной ошибкой:

"Commands out of sync; you can't run this command now"

Как я могу получить "настоящую ошибку" из SQL Alchemy?

(кто-то просил отследить, так что вот оно)

 File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 252, in execute
    self._warning_check()
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 148, in _warning_check
    warnings = db.show_warnings()
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 381, in show_warnings
    self.query("SHOW WARNINGS")
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")

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

Traceback (most recent call last):
  File "/tmp/foo.py", line 8, in <module>
    engine.execute(ddl_sql)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2075, in execute
    return connection.execute(statement, *multiparams, **params)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 942, in execute
    return self._execute_text(object, multiparams, params)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1104, in _execute_text
    statement, parameters
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
    cursor.execute(statement, parameters)
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 252, in execute
    self._warning_check()
  File "lib/python3.6/site-packages/MySQLdb/cursors.py", line 148, in _warning_check
    warnings = db.show_warnings()
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 381, in show_warnings
    self.query("SHOW WARNINGS")
  File "lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
sqlalchemy.exc.ProgrammingError: (_mysql_exceptions.ProgrammingError) (2014, "Commands out of sync; you can't run this command now") [SQL: 'DROP TABLE IF EXISTS TMP_junk_delete;\n\nCREATE TABLE TMP_junk_delete AS\nSELECT * \nFROM goals;\n\nDROP TABLE IF EXISTS TMP_junk_delete;\n\nCREATE TABLE TMP_junk_delete AS\nSELECT * \nFROM goals;\n\nDROP TABLE IF EXISTS TMP_junk_delete;\n'] (Background on this error at: http://sqlalche.me/e/f405)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...