我对sqlalchemy有一个非常混乱的问题。这不是我第一次使用sqlalchemy。我正在处理一个刚从头开始的新项目,因此可能存在配置错误。我在pip reqs文件中包含python连接器包,以防它感兴趣:
mysql-connector-repackaged==0.3.1
我刚刚创建了一个基本的单元测试来评估我们的sql包装器类。评估中的方法是add_user类,它只是将用户添加到数据库中。该类首先运行查询以查看用户是否存在。此查询未成功。
以下是查询的代码:
q = self.session.query(User).\
filter_by(name=name, email=email)
result = q.all()
看起来很简单吧?我会在下面包含我的连接字符串,以防有人感兴趣:
db =create_engine('mysql+mysqlconnector://{user}:{password}@{host}:{port}/{database}'.format(user=conn.USER, password=conn.PASS, host=conn.HOST, port=conn.PORT, database=database, poolclass=NullPool))
虽然表结构确实存在,但数据库为空。运行此查询时,它会引发异常。例外情况如下:
StatementError: expected string or buffer (original cause: TypeError: expected string or buffer) u"SHOW VARIABLES LIKE 'sql_mode'" []
此异常来自python正则表达式类re.py
。堆栈跟踪的结尾如下所示:
File "/home/vagrant/GitRepos/SqlInteraction/venv/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
如果我在那条线上突破,我可以看到问题所在。 _compile方法运行的字符串对象根本不是字符串。这是一个元组。元组看起来像这样:(0, 3, 1, '', '')
现在,虽然很明显为什么这个方法会破坏,但是为什么将元组传递给该方法并不明显。所有这些都发生在第三方库的内部。我在下面提供了完整的堆栈跟踪以供参考:
Traceback (most recent call last):
File "/home/vagrant/GitRepos/SqlInteraction/test/databasetest.py", line 14, in setUp
userId = self.add_user(Constants.TestObjects.USER_ID, Constants.TestObjects.USER_EMAIL, Constants.TestObjects.USER_PHONE)
File "/home/vagrant/GitRepos/SqlInteraction/test/databasetest.py", line 27, in add_user
result = q.all()
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2320, in all
return list(self)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2438, in __iter__
return self._execute_and_instances(context)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2451, in _execute_and_instances
close_with_result=True)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2442, in _connection_from_session
**kw)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 854, in connection
close_with_result=close_with_result)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 858, in _connection_for_bind
return self.transaction._connection_for_bind(engine)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 322, in _connection_for_bind
conn = bind.contextual_connect()
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1798, in contextual_connect
self.pool.connect(),
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 338, in connect
return _ConnectionFairy._checkout(self)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 644, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 440, in checkout
rec = pool._do_get()
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 963, in _do_get
return self._create_connection()
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 285, in _create_connection
return _ConnectionRecord(self)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 416, in __init__
exec_once(self.connection, self)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 250, in exec_once
self(*args, **kw)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 260, in __call__
fn(*args, **kw)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 1219, in go
return once_fn(*arg, **kw)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 166, in first_connect
dialect.initialize(c)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2453, in initialize
self._detect_ansiquotes(connection)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2718, in _detect_ansiquotes
connection.execute("SHOW VARIABLES LIKE 'sql_mode'"),
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 721, in execute
return self._execute_text(object, multiparams, params)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 870, in _execute_text
statement, parameters
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 893, in _execute_context
None, None)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1159, in _handle_dbapi_exception
exc_info
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 889, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 635, in _init_statement
if not dialect.supports_unicode_statements and \
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 725, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqlconnector.py", line 97, in supports_unicode_statements
return util.py3k or self._mysqlconnector_version_info > (2, 0)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 725, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/vagrant/GitRepos/SqlInteraction/venv/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqlconnector.py", line 131, in _mysqlconnector_version_info
self.dbapi.__version__)
File "/home/vagrant/GitRepos/SqlInteraction/venv/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
StatementError: expected string or buffer (original cause: TypeError: expected string or buffer) u"SHOW VARIABLES LIKE 'sql_mode'" []
我不知道为什么会出现这个问题。我在网上反弹没有任何有用的结果。我希望有人在这里可能会知道发生了什么。