Flask Flask-SQLAlchemy Installation and Initial Example



pip install Flask-SQLAlchemy

Simple Model

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    email = db.Column(db.String(120), unique=True)

The code example above shows a simple Flask-SQLAlchemy model, we can add an optional tablename to the model declaration however it is often not necessary as Flask-SQLAlchemy will automatically use the class name as the table name during database creation.

Our class will inherit from the baseclass Model which is a configured declarative base hence there is no need for us to explicitly define the base as we would when using SQLAlchemy.