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>Although I couldn't figure out why the error occurs, I know how as well as how to prevent it.  It is created because the query doesn't pull the live data right after the commit.  The way to prevent it is by using <code>db.session.query()</code>.  So in my example I would change:</p>

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

<p>to </p>

<p><code>f = db.session.query(File).filter_by(id=id).first()</code></p>

<p>For some reason that works.  Although I don't know why.</p>

<p><strong><em>EDIT</em></strong>: It seems to have to do with the class not receiving updated session.  For the time being I recommending using queries within session.</p>

python flask sqlalchemy flask-sqlalchemy flask-restful