Sqlalchemy类_MatchType(sqltypes.Float,sqltypes.MatchType):AttributeError

时间:2016-08-11 07:33:21

标签: mysql flask sqlalchemy flask-sqlalchemy

有时执行查询并收到此错误:

ct = db.session.query(CIType).filter(
                CIType.type_name == key).first() or \
                db.session.query(CIType).filter(CIType.type_id == key).first()

完整错误信息

2016-08-11 14:27:26,177 ERROR /usr/lib/python2.6/site-packages/flask/app.py 1306 - Exception on /api/v0.1/projects/search-indexer-rafael/product [GET]
    Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
File "/usr/lib/python2.6/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
File "/usr/lib/python2.6/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
File "/usr/lib/python2.6/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
File "/data/webapps/cmdb-api/core/special.py", line 175, in get_project_product
    product = ProjectManager().get_for_product(project_name)
File "/data/webapps/cmdb-api/lib/special/project.py", line 18, in __init__
    self.ci_type = CITypeCache.get("project")
File "/data/webapps/cmdb-api/models/cmdb.py", line 458, in get
    ct = db.session.query(CIType).filter(
File "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/scoping.py", line 149, in do
    def do(self, *args, **kwargs):
File "/usr/lib64/python2.6/site-packages/sqlalchemy/util/_collections.py", line 903, in __call__
    item = dict.get(self, key)
File "/usr/lib/python2.6/site-packages/flask_sqlalchemy.py", line 201, in __init__
    bind=db.engine,
File "/usr/lib/python2.6/site-packages/flask_sqlalchemy.py", line 754, in engine
    return self.get_engine(self.get_app())
File "/usr/lib/python2.6/site-packages/flask_sqlalchemy.py", line 771, in get_engine
    return connector.get_engine()
File "/usr/lib/python2.6/site-packages/flask_sqlalchemy.py", line 451, in get_engine
    self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/__init__.py", line 344, in create_engine
    of 0 indicates no limit; to disable pooling, set ``poolclass`` to
File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/strategies.py", line 50, in create

File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/url.py", line 116, in get_dialect
    return self.get_dialect().driver
File "/usr/lib64/python2.6/site-packages/sqlalchemy/util/langhelpers.py", line 170, in load
    fn.__func__.__doc__ = doc
File "/usr/lib64/python2.6/site-packages/sqlalchemy/dialects/__init__.py", line 33, in _auto_fn
    try:
File "/usr/lib64/python2.6/site-packages/sqlalchemy/dialects/mysql/__init__.py", line 8, in <module>
    from . import base, mysqldb, oursql, \
File "/usr/lib64/python2.6/site-packages/sqlalchemy/dialects/mysql/base.py", line 681, in <module>
    class _MatchType(sqltypes.Float, sqltypes.MatchType):
AttributeError: 'module' object has no attribute 'MatchType'

@special.route("/api/v0.1/projects/<string:project_name>/product",
           methods=["GET"])
def get_project_product(project_name):
    product = ProjectManager().get_for_product(project_name)
    return jsonify(product=product)
...

转到

class ProjectManager(object):
    def __init__(self):
        self.ci_type = CITypeCache.get("project")
...

然后

class CITypeCache(object):
    @classmethod
    def get(cls, key):
        if key is None:
            return
        ct = cache.get("CIType::ID::%s" % key) or \
            cache.get("CIType::Name::%s" % key)
        if ct is None:
            ct = db.session.query(CIType).filter(
                CIType.type_name == key).first() or \
                db.session.query(CIType).filter(CIType.type_id == key).first()
            if ct is not None:
                CITypeCache.set(ct)
        return ct

sqlalchemy的版本是 SQLAlchemy-1.0.8-py2.6.egg-info

并且经过多次相同的错误后,我再也无法捕捉到这个错误了。这个错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

我假设CIType.type_name和CIType.type_id具有不同的数据类型(可能是字符串和数字类型)。这可能导致以下情况:

db.session.query(CIType).filter(CIType.type_name == key).first()

是有效的表达式,但是:

db.session.query(CIType).filter(CIType.type_id == key).first()

因类型不匹配而产生错误。您需要将key转换为此表达式中的type_id列类型。

第一个表达式返回无结果时计算第二个表达式。正如Python documentation所述:

  

表达式x or y首先评估x;如果x为真,则返回其值;否则,评估y并返回结果值。

例如:

>>> a = 1 or 2 + '2'
>>> print a
1
>>> a = 0 or 2 + '2'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'