Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The create_api method fails if flask-sqlalchemy has been initialized with init_app() #409

Closed
TimotheeJeannin opened this issue Feb 24, 2015 · 10 comments
Labels

Comments

@TimotheeJeannin
Copy link
Contributor

Let's say I have two files:

server.py

import flask
import flask.ext.sqlalchemy
import flask.ext.restless
from model import db, Person, Computer

app = flask.Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

with app.app_context():
    db.init_app(app)
    db.drop_all()
    db.create_all()

manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])
manager.create_api(Computer, methods=['GET'])

app.run()

model.py

import flask.ext.sqlalchemy

db = flask.ext.sqlalchemy.SQLAlchemy()


class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    birth_date = db.Column(db.Date)


class Computer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    vendor = db.Column(db.Unicode)
    purchase_time = db.Column(db.DateTime)
    owner_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    owner = db.relationship('Person', backref=db.backref('computers',
                                                         lazy='dynamic'))

When I do a python server.py it works as expected with Flask-Restless version 0.16.0, but fails with Flask-Restless version 0.17.0.

Traceback (most recent call last):
  File "server.py", line 16, in <module>
    manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_restless/manager.py", line 698, in create_api
    blueprint = self.create_api_blueprint(app=app, *args, **kw)
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_restless/manager.py", line 566, in create_api_blueprint
    pk_name = primary_key or primary_key_name(model)
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_restless/helpers.py", line 232, in primary_key_name
    pk_names = primary_key_names(model)
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_restless/helpers.py", line 214, in primary_key_names
    return [key for key, field in inspect.getmembers(model)
  File "/usr/lib/python2.7/inspect.py", line 253, in getmembers
    value = getattr(object, key)
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 454, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 71, in __call__
    return self.registry()
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 878, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 704, in create_session
    return SignallingSession(self, **options)
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 149, in __init__
    self.app = db.get_app()
  File "/home/tim/Workspace/testRestless/env/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 845, in get_app
    raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
@pplanel
Copy link

pplanel commented Jun 8, 2015

Version 0.17.0 confirmed here, downgraded to 0.16.0 and worked.

@diegoponciano
Copy link

I've got the same problem with 0.17.0, although I didn't try it with 0.16.0

@nickjbyrne
Copy link

Downgrade to 0.16 worked for me

@darrenburns
Copy link

Same issue. Seems to work with fine if only using GET for me, as soon as I add POST to the methods it throws this error.

@darrenburns
Copy link

In 0.17 you can pass your app as a keyword argument to create_api() and remove it from the constructor of APIManager(). The following should work (although inconvenient):

rest_manager = APIManager(flask_sqlalchemy_db=db)
rest_manager.create_api(SomeModel, methods=['GET', 'POST'], app=app)

Edit: Nevermind, this isn't registering the endpoints for me.

@joergwork
Copy link

Same issue.
I tried the following and succeeded but can't explain why:

with app.app_context():
    rest_manager.create_api(SomeModel, methods=['GET', 'POST'])

@jklapuch
Copy link

I added next line

db.app = app

before

db.init_app(app)

and restless works.

@green4984
Copy link

finally, I found what it happened!

this is below with create_api is ok for me

manager.create_api(Person, methods=['GET', 'POST', 'DELETE'], primary_key='id')

I discover the source code of create_api_blueprint, add a primary_key will not check the db before
app context

@jfinkels
Copy link
Owner

jfinkels commented Feb 4, 2016

Hi all,

The init_app and Flask application registration was totally crazy before, since I had no idea what I was doing. It should be fixed now (in the development version). I apologize for that. Closing this now, but please reopen a new issue if you still have problems (since I completely rewrote the code).

@jfinkels jfinkels closed this as completed Feb 4, 2016
@jfinkels jfinkels added the bug label Feb 4, 2016
@sammarxz
Copy link

sammarxz commented Aug 8, 2017

Check if any route in your app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants