Skip to main content

Demo: application of Flask SQLAlchemy

Check the following link to review this demo of a complete Flask application.

app_fsqla.py

This demo works totally the same as the Lite demo. In other words, its APIs and features are totally the same as those of that demo. The only differences are:

  1. The default backend is powered by Flask SQLAlchemy.
  2. In the codes, we prefer to use the old-style database APIs provided by Flask SQLAlchemy.

Database structure

The entity-relationship diagram (ERD) of the database is also the same:

ERD of the regular application demoERD of the regular application demo

Compared to the demo of the lite version, this demo has such different features:

  • The default database backend is Flask SQLAlchemy. If the regular version is not available, fall back to Flask SQLAlchemy Lite.

  • For each object-relationship mapping (ORM) model, benefiting from the enhancement of Flask SQLAlchemy, we do not need to manually specify __tablename__.

  • The following APIs

    • db.session.execute(sa.select(...))
    • db.session.query(...)

    are replaced by the old-style APIs like Model.query. Such usages are provided by Flask SQLAlchemy.

  • The ID-based entity locating is implemented by db.get_or_404(...) rather than db.session.get(...). This get_or_404(...) method is provided by Flask SQLAlchemy. It will not return None but throw a 404 abort when the model is not found.

Access the demo

Use the same testing scripts in the Lite demo because the implementation of this demo has totally the same APIs of that one.