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?