SqlAlchemy: Could not locate column in row for col

2019-08-19 10:39发布

问题:

I have a problem with my endpoint in Falcon framework, I have two models Class with access to DB but when execute the seconds class, this raise an exception:

sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'translate'

I don't have a 'translate' column in my database or in my object mapping.

Version:

Python 3.6.2

PyMySQL==0.7.11

SQLAlchemy==1.1.11

My Code:

endpoint:

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

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

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]

Database

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),
...
) 

The error raise when execute the "new_user_exam" method. if the function 'find_rolecourseid' does not execute, the method "new_user_exam" works.

All trace:

 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'

Help and thks