alembic不会执行设置分隔符的原始sql

时间:2018-04-16 18:39:08

标签: python mysql sqlalchemy alembic

我试图通过在alembic的upgrade函数中执行原始SQL字符串来在我的数据库中创建一个触发器。但是,在运行upgrade时,我得到了sqlalchemy.exc.ProgrammingError:

在我的迁移脚本中运行:

def upgrade():
    connection = op.get_bind()
    connection.execute('DELIMITER #')

生成此追溯:

app/migrate.py:10: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead.
  from flask.ext.script import Manager
app/migrate.py:11: ExtDeprecationWarning: Importing flask.ext.migrate is deprecated, use flask_migrate instead.
  from flask.ext.migrate import Migrate, MigrateCommand
/home/my_project/app/database/models.py:2: ExtDeprecationWarning: Importing flask.ext.whooshalchemy is deprecated, use flask_whooshalchemy instead.
  import flask.ext.whooshalchemy
/home/my_project/venv/local/lib/python2.7/site-packages/flask_whooshalchemy.py:18: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead.
  import flask.ext.sqlalchemy as flask_sqlalchemy
/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py:1518: Warning: '@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead
  cursor.execute('SELECT @@tx_isolation')
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 60f73629ea28 -> 48dd7624f294, add trigger for adding to history table
Traceback (most recent call last):
  File "app/migrate.py", line 25, in <module>
    manager.run()
  File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/flask_migrate/__init__.py", line 244, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/command.py", line 254, in upgrade
    script.run_env()
  File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/script/base.py", line 416, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/util/compat.py", line 75, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "migrations/env.py", line 87, in <module>
    run_migrations_online()
  File "migrations/env.py", line 80, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/runtime/environment.py", line 817, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/alembic/runtime/migration.py", line 323, in run_migrations
    step.migration_fn(**kw)
  File "/home/my_project/migrations/versions/48dd7624f294_add_trigger_for_adding_to_history_table.py", line 22, in upgrade
    connection.execute('DELIMITER #')
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 939, in execute
    return self._execute_text(object, multiparams, params)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_text
    statement, parameters
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 469, in do_execute
    cursor.execute(statement, parameters)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/my_project/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
sqlalchemy.exc.ProgrammingError: (_mysql_exceptions.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER #' at line 1") [SQL: 'DELIMITER #']

由于某些原因DELIMETER是否可以connection.execute使用,或者我做错了什么?

相关版本:

pip:
- alembic==0.9.1
- Flask-SQLAlchemy==2.1
- MySQL-python==1.2.5
- SQLAlchemy==1.1.4
- SQLAlchemy-Utils==0.32.12
MySQL: 5.7.21
OS: ubuntu 16

0 个答案:

没有答案