Search code examples
pythonflaskflask-sqlalchemyflask-restless

Application Not Registered Error with flask-restless


I've been trying to make the code on https://github.com/graup/flask-restless-security work for a while. After I had failed to incorporate this in my own code I decided to simply just use this prepared code. However when I try to run server.py it raises the error below. Origin of the error is apimanager of flask-restless. I opened an issue on git however apparently the project is not maintained anymore. How can I make this work?

Traceback (most recent call last):
File "E:/WEB/Project1/app/server.py", line 62, in 
include_columns=['id', 'data1', 'data2', 'user_id'])
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\manager.py", line 698, in create_api
blueprint = self.create_api_blueprint(app=app, *args, **kw)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\manager.py", line 566, in create_api_blueprint
pk_name = primary_key or primary_key_name(model)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\helpers.py", line 232, in primary_key_name
pk_names = primary_key_names(model)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\helpers.py", line 214, in primary_key_names
return [key for key, field in inspect.getmembers(model)
File "c:\python27\Lib\inspect.py", line 252, in getmembers
value = getattr(object, key)
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 498, in get
return type.query_class(mapper, session=self.sa.session())
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in call
return self.registry()
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\util_collections.py", line 990, in call
return self.registry.setdefault(key, self.createfunc())
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\orm\session.py", line 2829, in call
return self.class_(**local_kw)
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 143, in init
self.app = app = db.get_app()
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 957, in get_app
'application not registered on db instance and no application'
RuntimeError: application not registered on db instance and no applicationbound to current context

server.py

from database import db
from application import app
from flask_restless import APIManager
from models import SomeStuff

apimanager = APIManager(app, flask_sqlalchemy_db=db)
apimanager.create_api(SomeStuff,
    methods=['GET', 'POST', 'DELETE', 'PUT'],
    url_prefix='/api/v1',
    collection_name='free_stuff',
    include_columns=['id', 'data1', 'data2', 'user_id'])

application.py

from flask import Flask

app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')

database.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

models.py

from database import db
from flask_security import UserMixin, RoleMixin, SQLAlchemyUserDatastore

class SomeStuff(db.Model):
    __tablename__ = 'somestuff'
    id = db.Column(db.Integer, primary_key=True)
    data1 = db.Column(db.Integer)
    data2 = db.Column(db.String(10))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
    user = db.relationship(User, lazy='joined', join_depth=1, viewonly=True)

Solution

  • The error you have now:

    flask_sqlalchemy_init_.py RuntimeError: application not registered on db instance and no applicationbound to current context

    is at flask_sqlalchemy usage, that not define a app instance.

    The solution is register the application on db instance.

    from flask_sqlalchemy import SQLAlchemy
    from application import app
    
    db = SQLAlchemy(app)