Receiving AssertionError using Flask sqlalchemy and restful
<p>I have been stumped and can't seem to figure out why I am receiving an AssertionError.  I am currently working on a rest api using the flask_restful lib.  I am querying by:</p>

<pre><code>@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)&gt;=4:
            return f
        print f.check_permission(user_id)
        FileErrors.InsufficientFilePermission()
    FileErrors.FileDoesNotExist()
</code></pre>

<p>The error message looks like this:</p>

<pre><code>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
</code></pre>

<p>This is how my File model looks like:</p>

<pre><code>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)&gt;=4:
            return f
        print f.check_permission(user_id)
        FileErrors.InsufficientFilePermission()
    FileErrors.FileDoesNotExist()
</code></pre>

<p>Any help would be appreciated.  Thanks in advance.</p>

<p>Are you sure you want to have an assignment operator instead of comparison in your filter.  Try replacing = with == and see if it solves your problem. </p>

<pre><code>f = File.query.filter_by(id == id).first()
</code></pre>

<p>Hannu</p>

python flask sqlalchemy flask-sqlalchemy flask-restful