Every table in the database must be created. This is done before the Flask server is started otherwise it causes errors.
I’ll be using the F5 Info page.

Creating the Table Models.py
The models.py file is what contains all the Python classes for the database tables.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from website import db, login_manager from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin class F5Info(db.Model): __tablename__ = 'f5_info_table' id = db.Column(db.Integer,primary_key=True) hostname = db.Column(db.Text) ip = db.Column(db.Text) serial_number = db.Column(db.Text) version = db.Column(db.Text) failover_active = db.Column(db.Text) failover_mate = db.Column(db.Text) failover_status = db.Column(db.Text) failover_lastmsg = db.Column(db.Text) lastCheck = db.Column(db.Text) updated = db.Column(db.Text) |
Setting up the Database
This must be run before the Flask application will work correctly. I’m sure there will be a way to create the database tables automatically, but for now I am using this method as you only have to do it when adding new tables.
0 1 2 3 4 5 6 |
export FLASK_APP=app.py #Mac set FLASK_APP=app.py or $env:FLASK_APP = "app" #Windows flask db init flask db migrate -m "new tables" flask db upgrade |
Drawbacks
As I said this must be ran when you add a new table. This also goes fore adding new fields to an existing table.
The database must be deleted and then recreated. So all the data is lost in all tables. This hasn’t been an issue for me in this project.
Files to delete;
- data.sqlite (this may be named differently, it should be under__init__.py
- migrations directory
0 1 2 3 4 5 6 7 8 9 10 |
app = Flask(__name__) basedir = os.path.abspath(os.path.dirname(__file__)) app.config['SECRET_KEY'] = 'mykey' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) Migrate(app,db) |