使用Flask sqlalchemy和restful接收AssertionError

时间:2016-10-04 06:04:11

标签: python flask sqlalchemy flask-sqlalchemy flask-restful

我被困住了,似乎无法弄清楚为什么我收到AssertionError。我目前正在使用flask_restful lib来处理rest api。我在查询:

@staticmethod
def find_by_id(id, user_id):
    f = File.query.filter_by(id=id).first() #Error is happening here
    if f is not None:
        if f.check_permission(user_id)>=4:
            return f
        print f.check_permission(user_id)
        FileErrors.InsufficientFilePermission()
    FileErrors.FileDoesNotExist()

错误消息如下所示:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 268, in error_router
    return self.handle_error(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1531, in handle_user_exception
    assert exc_value is e
AssertionError

这就是我的文件模型的样子:

class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer)
parts = db.Column(db.Integer)
size = db.Column(db.Integer)
name = db.Column(db.String(100))

def __init__ (self, file_info):
    self.user_id = file_info['user_id']
    self.parts = file_info['parts']
    self.size = file_info['size']
    self.name = file_info['name']

@staticmethod
def create(file_info):
    return add_to_db(File(file_info))

@staticmethod
def delete(file_id, user_id):
    pass

def check_permission(self,user_id):
    permission = 0
    print 'self.user_id {}'.format(self.user_id)
    print 'user_id {}'.format(user_id)
    if self.user_id == user_id:
        return 7
    fs = FileShare.find_by_file_and_user_id(self.id, user_id)
    if fs is not None:
        permission = fs.permission
    return permission

@staticmethod
def find_by_id(id, user_id):
    f = File.query.filter_by(id=id).first() #Error is happening here
    if f is not None:
        if f.check_permission(user_id)>=4:
            return f
        print f.check_permission(user_id)
        FileErrors.InsufficientFilePermission()
    FileErrors.FileDoesNotExist()

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您确定要在过滤器中使用赋值运算符而不是比较。尝试使用==替换=并查看它是否解决了您的问题。

f = File.query.filter_by(id == id).first()

哈努哈利

答案 1 :(得分:0)

虽然我无法弄清楚错误发生的原因,但我知道如何预防错误。创建它是因为查询在提交后没有立即提取实时数据。防止它的方法是使用db.session.query()。所以在我的例子中我会改变:

f = File.query.filter_by(id=id).first()

f = db.session.query(File).filter_by(id=id).first()

出于某种原因,这有效。虽然我不知道为什么。

编辑 :这似乎与未接收更新会话的类有关。目前我建议在会话中使用查询。