0Pricing
Flask Academy · Lesson

Install and Configure the Extension

Set the database URI and init SQLAlchemy.

Install and Configure the Extension is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why an ORM at All

Writing raw SQL by hand gets messy fast. An ORM lets you work with Python objects and rows feel like normal classes. 🗃️

Meet Flask-SQLAlchemy

Flask-SQLAlchemy is the official extension that wires SQLAlchemy into Flask, so your app gets a clean, ready-to-use database layer.

Install the Package

You add it like any dependency. pip pulls in Flask-SQLAlchemy and the SQLAlchemy core it builds on automatically.

pip install Flask-SQLAlchemy

Create the db Object

You make one SQLAlchemy instance, usually named db. Every model and query you write later will hang off this single object.

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

The Database URI

Flask needs to know where your data lives. The SQLALCHEMY_DATABASE_URI setting is a connection string pointing at your database.

A Simple SQLite URI

For learning, SQLite is perfect: it lives in one file, needs no server, and the URI is just a path on disk.

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///app.db"

Bind db to the App

After config, you connect the extension to your app with init_app. This hands Flask-SQLAlchemy everything it needs to run.

db.init_app(app)

Two Ways to Initialize

You can pass the app straight to SQLAlchemy(app) for tiny apps, or use init_app later to keep db reusable across setups.

Swap Databases Easily

Only the URI changes between SQLite, PostgreSQL, or MySQL. Your models and queries stay identical, which makes switching painless.

"postgresql://user:pass@localhost/mydb"

Skip the Modification Tracker

Set SQLALCHEMY_TRACK_MODIFICATIONS to False. It silences a warning and avoids extra overhead you almost never need.

app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

One Extension, Many Tools

Once configured, db gives you Model for tables, Column for fields, and session for saving. You will meet each of these soon.

Quick Check

Which config key tells Flask-SQLAlchemy where your database lives?

Recap

You installed the extension, created a db object, set the URI, and bound it with init_app. Your app is ready for models. 🎉

Frequently asked questions

Is the “Install and Configure the Extension” lesson free?

Yes — the full text of “Install and Configure the Extension” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Install and Configure the Extension”?

Set the database URI and init SQLAlchemy. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Install and Configure the Extension” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Install and Configure the Extension
  2. Define Your First Model Class
  3. Columns, Types, and Constraints
  4. Create the Schema with create_all
← Back to Flask Academy