Skip to content

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

Closed
@TimotheeJeannin

Description

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

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions