Creating models in multiple models.py for a flask blueprint app at startup

Viewed 27

I'm unsure of how/when the models.py files get picked up by flask on start up for a multiple module app. I would like the app to create missing tables on start up but it doesn't seem to do that here.

The repo is here https://github.com/CodeHostedHere/channel_response_bot

The models file that isn't being seen is here https://github.com/CodeHostedHere/channel_response_bot/blob/master/slash_commands/models.py

Database is created here, imported to extensions and imported around the app https://github.com/CodeHostedHere/channel_response_bot/blob/master/database.py

1 Answers

I took 20 minutes to restructure things to match this Digital Ocean tutorial and it all works now. The DB is created at the top most __init__.py and then imported around the app. This was made possible by

# Define the application directory
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__)

Previously this hadn't worked for me. I actually needed to have the file I run to start flask, in a directory that doesn't have its own __init__.py. I believe missing this parent directory was why my initial attempts failed.

Truth be told, this tutorial is actually also missing the line to commit to the database:

db.session.commit() directly after db.create_all()

I can now structure my flask app in to separate logical components and share the db connection across them. I had avoided the Ginsberg tutorial previously because it is just a single models.py

Related