Support Online
Skip to main content

Using Flask SQLAlchemy

In this guide, you will learn how to manage database operations in Flask applications with SQLAlchemy.
Model definition, adding, listing, updating and deleting records are discussed.

Technical Summary

This guide explains how to use Flask-SQLAlchemy.
The goal is to facilitate persistent data management in Flask projects.
The steps are installation, model definition, CRUD and migration.


Prerequisites

  • A system with Python 3 installed
  • Flask basic knowledge
  • Use of virtual environment (virtualenv)

1. Flask and Flask-SQLAlchemy Installation

Install the required packages:

pip install Flask Flask-SQLAlchemy
  • This command installs Flask and ORM plugin.

2. Creating a Database Connection

  1. Open the application file:

nano app.py

  1. Define SQLite connection:

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
  • This setting creates a local database file.

3. Model (Table) Definition

  1. Sample student model:

class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(80), unique=True)
  • This class represents a database table.

4. Creating a Database and Table

  1. Start Flask shell:

flask shell

  1. Create the tables:

db.create_all()
  • This command produces tables according to model definitions.

5. Adding a Record (Create)

  1. Example of adding a new record:

student = Student(firstname="Ali", email="ali@ornek.com")
db.session.add(student)
db.session.commit()
  • This process makes the data permanent.

6. Listing Records (Read)

  1. Pull all records:

Student.query.all()
  • This command returns the table contents.

7. Registration Update

  1. Change existing data:

student.email = "ali2@ornek.com"
db.session.commit()
  • This action updates the relevant record.

8. Delete Record

  1. Data deletion example:

db.session.delete(student)
db.session.commit()
  • This action removes the record from the database.

9. Migration Management (Flask-Migrate)

  1. Install Flask-Migrate for schema changes:

pip install Flask-Migrate

  • Create the first migration:

flask db init
flask db migrate -m "initial"
flask db upgrade
  • These steps apply table changes safely.

Performance and Tips

  1. Use limit() on large lists

  2. Don't query unnecessary columns

  3. Speed up queries by adding indexes


Frequently Asked Questions

1. What does Flask-SQLAlchemy do? It manages database operations with Python objects.

2. Can I use PostgreSQL instead of SQLite? Yes, you just need to change the connection address.

3. Why is migration necessary? It updates the table structure without losing data.

4. Does ORM reduce performance? Yes, if used incorrectly, there is no problem with correct queries.


Result

With this guide, you have set up a complete CRUD infrastructure using Flask-SQLAlchemy. Your code has become database independent and scalable.

You can safely use this structure in Flask services running on GenixNode.