我尝试使用flask_sqlalchemy
创建表。我从flask_sqlalchemy
网站复制了演示代码。该代码在一周前运行良好。但它无法连接。 flask_sqlalchemy
和Mysql的版本是最新的。
我在Windows上运行代码。
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =
"mysql+pymysql://root:password@127.0.0.1/python"
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'user'
username = db.Column(db.String(80), unique=True,primary_key=True)
pw_hash = db.Column(db.String(80))
db.create_all()
Traceback (most recent call last):
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 1122, in _do_get
return self._pool.get(wait, self._timeout)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\util\queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last):
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 387, in connect
return _ConnectionFairy._checkout(self)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\pool.py", line 661, in __connect
exec_once(self.connection, self)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\event\attr.py", line 246, in exec_once
self(*args, **kw)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\event\attr.py", line 256, in __call__
fn(*args, **kw)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\util\langhelpers.py", line 1331, in go
return once_fn(*arg, **kw)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\engine\strategies.py", line 181, in first_connect
dialect.initialize(c)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\dialects\mysql\base.py", line 1682, in initialize
default.DefaultDialect.initialize(self, connection)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\engine\default.py", line 256, in initialize
self.get_isolation_level(connection.connection)
File "D:\python 3.6.6\lib\site-packages\sqlalchemy\dialects\mysql\base.py", line 1546, in get_isolation_level
cursor.execute('SELECT @@tx_isolation')
File "D:\python 3.6.6\lib\site-packages\pymysql\cursors.py", line 165, in execute
result = self._query(query)
File "D:\python 3.6.6\lib\site-packages\pymysql\cursors.py", line 321, in _query
conn.query(q)
File "D:\python 3.6.6\lib\site-packages\pymysql\connections.py", line 860, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "D:\python 3.6.6\lib\site-packages\pymysql\connections.py", line 1061, in _read_query_result
result.read()
File "D:\python 3.6.6\lib\site-packages\pymysql\connections.py", line 1349, in read
first_packet = self.connection._read_packet()
File "D:\python 3.6.6\lib\site-packages\pymysql\connections.py", line 1018, in _read_packet
packet.check_error()
File "D:\python 3.6.6\lib\site-packages\pymysql\connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "D:\python 3.6.6\lib\site-packages\pymysql\err.py", line 107, in
raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1193, "Unknown system variable 'tx_isolation'")