0

I'm trying to extend the flask-base project https://github.com/hack4impact/flask-base/tree/master/app In this the app/_init.py initializes the flask app with multiple extensions including sqlalchemy:

mail = Mail()
db = SQLAlchemy()
.....

def create_app(config_name):
    ....
    db.init_app(app)
    login_manager.init_app(app)
    ...

But then separately the data models are defined in app/models, eg in app/models.user.py I have:

class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    confirmed = db.Column(db.Boolean, default=False)
    first_name = db.Column(db.String(64), index=True)
    last_name = db.Column(db.String(64), index=True)
    ....

I want to create a session object that has the ability to query all the tables in the db, and use that in my view like in https://www.pythoncentral.io/understanding-python-sqlalchemy-session/ but this example is best in cases where there is a single file, not a more distributed setup like in this relatively complex flask app. I'm trying to follow the ideas in Flask and SQLAlchemy, application not registered on instance

In app/models/init.py , I have

"""
These imports enable us to make all defined models members of the models
module (as opposed to just their python files)
"""

from .user import *  # noqa
from .miscellaneous import *  # noqa
from .out import *  # noqa
from .properties import *  # noqa
from .accounts import *  # noqa

In main.views I have:

from app import db
from ..models import *



s = db.session()
print('hi')

@main.route('/')
def index():
    return render_template('main/index.html')

I'm getting the error above. I'm not sure exactly how to put the pieces together to get a session object with access to all the tables in the db

edit:

based on the answer I tried adding the following to one of my view functions:

db.session.add(Postings)
recordset = db.session.add(Postings).query.all()

Now I'm getting:

sqlalchemy.orm.exc.UnmappedInstanceError: Class 'flask_sqlalchemy._BoundDeclarativeMeta' is not mapped; was a class (app.models.out.Postings) supplied where an instance was required?
user1592380
  • 34,265
  • 92
  • 284
  • 515

1 Answers1

2

If you want to learn SQLAlchemy session basics, starting from relatively complex implementation of Flask project (with app factory) isn't actually very good idea, as most of the things there are done for you automatically and you're little bit limited with your own implementations.

The biggest problem with your code is, that app factory model was created to bond application with database and other extensions as late as possible. That's why in create_app function app is initialized, bond to database and views are registered.

So if you're trying to use db object from app, which is initialized, but not yet bond to app (because this whole view is registered in create_app function and your code is executed as soon as view try to import file with your code) it's going to throw exception.

Solution? You should use db object inside a view and use default session that is made for you. See app/admin/views in repo that you've linked for reference.

If you want to play with Flask and SQLAlchemy yourself - you should rather go for your own minimal app from scratch and use well-tailored specific implementation when you'll be sure that they suit your needs.

erhesto
  • 1,176
  • 7
  • 20
  • Well, that’s completely different problem, isn’t it? Don’t extend this question till infinity - if answer to original question fix your original problem - accept it and ask another. SO is not for interactive tutorials. – erhesto Feb 01 '18 at 06:34
  • Ok, We'll do it your way. – user1592380 Feb 01 '18 at 16:12
  • If you want to take a look, I've put up a follow up at https://stackoverflow.com/questions/48567309/class-flask-sqlalchemy-bounddeclarativemeta-is-not-mapped – user1592380 Feb 01 '18 at 20:17