使用 Flask sqlalchemy 和 restful 接收 AssertionError

Receiving AssertionError using Flask sqlalchemy and restful

我被难住了,似乎无法弄清楚为什么我会收到 AssertionError。我目前正在使用 flask_restful 库进行休息 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()

如有任何帮助,我们将不胜感激。提前致谢。

您确定要在筛选器中使用赋值运算符而不是比较运算符吗?尝试将 = 替换为 == 并查看是否可以解决您的问题。

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

汉奴

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

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

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

出于某种原因,这行得通。虽然不知道为什么。

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