title: flask-sqlalchemy: AttributeError: type object has no attribute 'query', works in ipython
body: <p>I'm creating a new flask app using flask-sqlalchemy and flask-restful with Python 3.4.  I've defined my User model as such:</p>
  
  <pre><code>from mytvpy import db
  from sqlalchemy.ext.declarative import declared_attr
  
  
  class BaseModel(db.Model):
      __abstract__ = True
  
      id = db.Column(db.Integer, primary_key=True)
      created = db.Column(db.TIMESTAMP, server_default=db.func.now())
      last_updated = db.Column(db.TIMESTAMP, server_default=db.func.now(), onupdate=db.func.current_timestamp())
  
      @declared_attr
      def __tablename__(cls):
          return cls.__name__
  
  
  class User(BaseModel):
  
      username = db.Column(db.String(255), unique=True)
      password = db.Column(db.String(255))
      email = db.Column(db.String(255), unique=True)
  
      def __init__(self, username, password, email):
          super(User, self).__init__()
          self.username = username
          self.password = password
          self.email = email
  </code></pre>
  
  <p>If I try to query in ipython, it works:</p>
  
  <pre><code>In [15]: from mytvpy.models.base import User
  
  In [16]: User.query.all()
  Out[16]: [&lt;mytvpy.models.base.User at 0x7fac65b1c6a0&gt;]
  </code></pre>
  
  <p>But if I try to hit it from an endpoint:</p>
  
  <pre><code>class User(Resource):
  
      def get(self, user):
          return User.query.filter(User.username==user).scalar()
  </code></pre>
  
  <p>It craps out:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
      return self.wsgi_app(environ, start_response)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
      response = self.make_response(self.handle_exception(e))
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
      return original_handler(e)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
      raise value.with_traceback(tb)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
      response = self.full_dispatch_request()
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
      return original_handler(e)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
      raise value.with_traceback(tb)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
      rv = self.dispatch_request()
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 471, in wrapper
      resp = resource(*args, **kwargs)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/views.py", line 84, in view
      return self.dispatch_request(*args, **kwargs)
    File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 581, in dispatch_request
      resp = meth(*args, **kwargs)
    File "/home/rich/prj/mytv/mytvpy/blueprints/base.py", line 12, in get
      return User.query.filter(User.username==user).scalar()
  AttributeError: type object 'User' has no attribute 'query'
  </code></pre>