0Pricing
Flask Academy · Lesson

Define Relationships and Foreign Keys

Link models with one-to-many associations.

Define Relationships and Foreign Keys is a free Flask Academy lesson on CoddyKit — lesson 3 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.

Rows That Belong Together

Real data connects: a user has many posts, an order has many items. A relationship links these rows in your models. 🔗

The Foreign Key

A foreign key is a column on the child that stores the parent's id. It is the actual link saved in the database.

user_id = db.Column(db.Integer, db.ForeignKey("user.id"))

Point at the Right Table

Inside ForeignKey you name the target as table.column, using the lowercase table name Flask-SQLAlchemy generates.

db.ForeignKey("user.id")

Add a relationship

The relationship() helper turns that key into easy navigation, so you reach linked objects as attributes.

posts = db.relationship("Post")

One-to-Many in Practice

Put the foreign key on the many side and the relationship on the one side. One user, many posts.

class Post(db.Model):
    user_id = db.Column(db.ForeignKey("user.id"))

Navigate Parent to Children

Once linked, read a parent's children like a list. SQLAlchemy fetches them for you on access. ✨

user = User.query.first()
for p in user.posts:
    print(p.title)

Walk Back with backref

Add backref to a relationship and the child instantly gains a way to reach its parent.

posts = db.relationship("Post", backref="author")

Use the Backref

That backref lets you go from a child straight back to its owner with a clean attribute. 🙌

post = Post.query.first()
print(post.author.name)

Assign by Object

Link rows by setting the relationship attribute to an object. SQLAlchemy fills in the foreign key for you.

post = Post(title="Hi")
post.author = user

Cascade on Delete

Set a cascade so deleting a parent can also remove its children, keeping the database tidy and consistent.

db.relationship("Post", cascade="all, delete")

Key vs Relationship

The foreign key stores the link in SQL; relationship is the Python convenience layer on top of it. You usually want both.

Quick Check

One user can write many posts. Where does the foreign key column live?

Recap: Linking Models

You now connect tables with a foreign key, navigate them via relationship and backref, and control deletes with cascade. Models, united! 🎉

Frequently asked questions

Is the “Define Relationships and Foreign Keys” lesson free?

Yes — the full text of “Define Relationships and Foreign Keys” 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 Relationships and Foreign Keys”?

Link models with one-to-many associations. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Define Relationships and Foreign Keys” 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. Fetch Rows with query and get
  2. Filter, Order, and Limit Results
  3. Define Relationships and Foreign Keys
  4. Joins and Lazy vs Eager Loading
← Back to Flask Academy