0Pricing
Flask Academy · Lesson

Define Your First Model Class

Map a Python class to a database table.

Define Your First Model Class is a free Flask Academy lesson on CoddyKit — lesson 2 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.

A Model Is a Table

In the ORM world, one Python class equals one database table. Each model describes the shape of the rows it will store. 🧱

Inherit from db.Model

Your class subclasses db.Model. That single base class gives it table powers like querying and saving for free.

class User(db.Model):
    pass

Class Name to Table Name

By default the table name is the lowercase class name. So a class named User maps to a table called user automatically.

Columns Are Class Attributes

Each field is a class attribute set to db.Column. The column type, like Integer or String, tells the database what to store.

name = db.Column(db.String(80))

Every Table Needs a Key

One column should be the primary key: a unique id for each row. It is how the database tells your records apart.

id = db.Column(db.Integer, primary_key=True)

Your First Full Model

Put it together: a key plus a couple of fields. This tiny class fully describes a User table and its columns.

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

Override the Table Name

Want a custom table name? Set __tablename__ on the class and the ORM uses that instead of the default.

__tablename__ = "users"

Create Instances Like Objects

A model instance is just a normal Python object. You pass column values as keyword arguments, and each becomes a row to save.

u = User(name="Ada")

Add a Helpful repr

Defining __repr__ makes objects print clearly in the shell and logs, which saves you guessing during debugging.

def __repr__(self):
    return f"<User {self.name}>"

Models Live in One Place

Keep models in a models.py file so they import cleanly into views, scripts, and tests without circular imports.

No SQL in Sight

Notice you never wrote CREATE TABLE. The model declaration is the schema, and the ORM translates it to SQL for you.

Quick Check

What does a Flask-SQLAlchemy model class represent?

Recap

You built a model by subclassing db.Model, declared columns, set a primary key, and learned that the class itself is your schema. ✅

Frequently asked questions

Is the “Define Your First Model Class” lesson free?

Yes — the full text of “Define Your First Model Class” 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 “Define Your First Model Class”?

Map a Python class to a database table. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Define Your First Model Class” 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