Mysterious error with sqlalchemy: TypeError 'User' object is not callable
<p>I am trying to create an simple app using flask and sqlalchemy linked to a MySQL database. I have created a User table in my database with some dummy data and am trying to test out a sample query on my /api route. I keep getting an internal server error, and for the life of me can't figure out what is happening. I've made an app similar to this before and it never gave me this strange error. The stack trace is as follows:</p>

<pre><code>2016-12-02 22:15:16,079 - app - ERROR - Exception on /api [GET]
    Traceback (most recent call last):
      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 1642, in full_dispatch_request
        response = self.make_response(rv)
      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1746, in make_response
        rv = self.response_class.force_type(rv, request.environ)
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 847, in force_type
        response = BaseResponse(*_run_wsgi_app(response, environ))
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
        return _run_wsgi_app(*args)
      File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 871, in run_wsgi_app
        app_rv = app(environ, start_response)
    TypeError: 'User' object is not callable
</code></pre>

<p><strong>app.py</strong></p>

<pre><code>from flask import Flask
from database.models import User
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import create_engine

engine = create_engine('mysql://root:123pass@localhost/testdb', convert_unicode=True)

Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Create a scoped session
ssession = scoped_session(Session)

app = Flask(__name__)


# Setup our error logging
if app.debug is not True:
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler('errors.log', maxBytes=1024 * 1024 * 100, backupCount=20)
    file_handler.setLevel(logging.ERROR)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    file_handler.setFormatter(formatter)
    app.logger.addHandler(file_handler)


@app.route('/')
def index():
    return 'hello world'


@app.route('/api')
def api_route():
    # Error is happening right here
    return ssession.query(User).first()


if __name__ == '__main__':
    app.run(debug=False)
</code></pre>

<p><strong>database/models.py</strong></p>

<pre><code>from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, BLOB, \
    DateTime, ForeignKey

Base = declarative_base()

class User(Base):
    __tablename__ = 'User'

    id = Column(Integer, primary_key=True)
    email = Column(String(64), unique=True)
    password = Column(String(255))
    join_date = Column(DateTime)
    last_login = Column(DateTime)
    login_count = Column(Integer, default=0)
    name = Column(String(128), default=None)
    location = Column(String(128), default=None)
    points = Column(Integer, default=0)
    num_posts = Column(Integer, default=0)

    def __init__(self, email, password):
        self.email = email
        self.password = password
        self.join_date = datetime.utcnow()
        self.last_login = datetime.utcnow()

    def __repr__(self):
        return '&lt;User %r&gt;' % self.email
</code></pre>

<p>I have no clue whatsoever as to why this is not working-- it actually works completely fine in the python console if I import all the necessary modules! Any insight would be awesome.</p>