SqlAlchemy:找不到列'translate'

时间:2017-07-25 00:12:55

标签: python mysql sqlalchemy falconframework

我的Falcon框架中的端点出现问题,我有两个可以访问DB的模型类,但是当执行seconds类时,会引发异常:

  

sqlalchemy.exc.NoSuchColumnError:“找不到列''''的列中的列

我的数据库或对象映射中没有“翻译”列。

版本:

  

Python 3.6.2

     

PyMySQL == 0.7.11

     

的SQLAlchemy == 1.1.11

我的代码:

端点:

def on_post(self, req, resp, courseid, examid): 

    with self.db.connect() as cnn:
        mdl_party = Party(cnn)
        mdl_exam = Exam(cnn)

        rolecourseid = mdl_party.find_rolecourseid(roleid,courseid,examid)
        if rolecourseid is None :
            raise AppValidationEx("ExamDoesntExist") 

        take_exam_id = mdl_exam.new_user_exam(username, rolecourseid, examid)

班级派对

class Party(object): 

def __init__(self, dbconnection):
    self.cnn = dbconnection

def find_rolecourseid(self, roleid, courseid, examid): 
    s = text(''' SELECT ... ''')

    cursor = self.cnn.execute(s,
        p_roleid=roleid,
        p_courseid=courseid,
        p_examid=examid)
    return cursor.first()

课堂考试

class Exam(object):
""" a user class """

def __init__(self, dbconnection):
    self.cnn = dbconnection

def new_user_exam(self, username, rolecourseid, examid): 
    now = datetime.utcnow()

    data = {
        'partyRoleCourseId': rolecourseid,
        'examTakeDate': now,
        'examTakeStart': now,
        'examId': examid,
        'createdBy': username,
        'createdDate': now,
        'deleted': 0,
        'version': 0
    }
    ins = partyrolecourseexam.insert().values(data) 
    cursor = self.cnn.execute(ins) #this raise exception
    take_exam_id = cursor.inserted_primary_key[0]

数据库

engine = create_engine("mysql+pymysql://%s:%s@%s/%s?charset=utf8" % DBPARAMS, 
pool_size=50, 
max_overflow=100,
echo=False)

metadata = MetaData() 

partyrolecourseexam = Table('partyrolecourseexam', metadata,
Column("id", Integer, primary_key=True),
Column("partyRoleCourseId", Integer),
...
) 

执行“new_user_exam”方法时出错。 如果函数'find_rolecourseid'没有执行,方法“new_user_exam”就可以了。

所有追踪:

 Traceback (most recent call last):
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 73, in __getitem__
     processor, obj, index = self._keymap[key]
 KeyError: 'translate'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 99, in __getattr__
     return self[name]
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 75, in __getitem__
     processor, obj, index = self._parent._key_fallback(key)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 563, in _key_fallback
     expression._string_or_unprintable(key))
 sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'translate'"

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\web.py", line 1488, in _execute
     result = self.prepare()
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\web.py", line 2774, in prepare
     self.fallback(self.request)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\wsgi.py", line 277, in __call__
     WSGIContainer.environ(request), start_response)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 230, in __call__
     if not self._handle_exception(ex, req, resp, params):
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 657, in _handle_exception
     err_handler(ex, req, resp, params)
   File "app_rest.py", line 17, in handle_errors
     raise ex
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 227, in __call__
     responder(req, resp, **params)
   File "C:\Users\USER\dev\clexam\controllers\exam_take.py", line 32, in on_post
     take_exam_id = mdl_exam.new_user_exam(username, rolecourseid, examid)
   File "C:\Users\USER\dev\clexam\models\exam.py", line 43, in new_user_exam
     cursor = self.cnn.execute(ins)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
     return meth(self, multiparams, params)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
     return connection._execute_clauseelement(self, multiparams, params)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
     compiled_sql, distilled_params
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
     context)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1405, in _handle_dbapi_exception
     util.reraise(*exc_info)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
     raise value
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
     context)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
     cursor.execute(statement, parameters)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 164, in execute
     query = self.mogrify(query, args)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 143, in mogrify
     query = query % self._escape_args(args, conn)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 123, in _escape_args
     return dict((key, conn.literal(val)) for (key, val) in args.items())
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 123, in <genexpr>
     return dict((key, conn.literal(val)) for (key, val) in args.items())
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\connections.py", line 821, in literal
     return self.escape(obj, self.encoders)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\connections.py", line 814, in escape
     return escape_item(obj, self.charset, mapping=mapping)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 27, in escape_item
     val = encoder(val, mapping)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 110, in escape_unicode
     return u"'%s'" % _escape_unicode(value)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
     return value.translate(_escape_table)
   File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 101, in __getattr__
     raise AttributeError(e.args[0])
 AttributeError: Could not locate column in row for column 'translate'

帮助和帮助

0 个答案:

没有答案