执行python manage.py db migrate -m 'init tables'
命令之后报了如下错误
Traceback (most recent call last):
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
return fn()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 516, in checkout
rec = pool._do_get()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 1229, in _do_get
return self._create_connection()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 661, in __connect
exec_once(self.connection, self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/event/attr.py", line 246, in exec_once
self(*args, **kw)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
fn(*args, **kw)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 1331, in go
return once_fn(*arg, **kw)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 181, in first_connect
dialect.initialize(c)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1714, in initialize
default.DefaultDialect.initialize(self, connection)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 256, in initialize
self.get_isolation_level(connection.connection)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1569, in get_isolation_level
cursor.execute('SELECT @@tx_isolation')
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1193, "Unknown system variable 'tx_isolation'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
manager.run()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_script/__init__.py", line 417, in run
result = self.handle(argv[0], argv[1:])
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_script/__init__.py", line 386, in handle
res = handle(*args, **config)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_migrate/__init__.py", line 95, in wrapped
f(*args, **kwargs)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/flask_migrate/__init__.py", line 215, in migrate
version_path=version_path, rev_id=rev_id)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/command.py", line 197, in revision
script_directory.run_env()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/script/base.py", line 475, in run_env
util.load_python_file(self.dir, "env.py")
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 90, in load_python_file
module = load_module_py(module_id, path)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/alembic/util/compat.py", line 156, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "migrations/env.py", line 95, in <module>
run_migrations_online()
File "migrations/env.py", line 80, in run_migrations_online
with connectable.connect() as connection:
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2091, in connect
return self._connection_cls(self, **kwargs)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2177, in raw_connection
self.pool.unique_connection, _connection)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
return fn()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 328, in unique_connection
return _ConnectionFairy._checkout(self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 516, in checkout
rec = pool._do_get()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 1229, in _do_get
return self._create_connection()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/pool.py", line 661, in __connect
exec_once(self.connection, self)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/event/attr.py", line 246, in exec_once
self(*args, **kw)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
fn(*args, **kw)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 1331, in go
return once_fn(*arg, **kw)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 181, in first_connect
dialect.initialize(c)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1714, in initialize
default.DefaultDialect.initialize(self, connection)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 256, in initialize
self.get_isolation_level(connection.connection)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1569, in get_isolation_level
cursor.execute('SELECT @@tx_isolation')
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/Users/zhangbin/py_envs/flask/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1193, "Unknown system variable 'tx_isolation'")
原因
以前版本的mysql使用的是tx_isolation
,现在版本的mysql8.0 被替换成了transaction_isolation
https://www.cnblogs.com/richiewlq/p/9916306.html
解决办法
升级 sqlalchemy
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade sqlalchemy --ignore-installed