Relationships and Foreign Keys
Connect tables together using foreign keys to model one-to-many and many-to-many relationships in your Supabase Postgres database.
Relationships and Foreign Keys is a free Supabase Backend as a Service lesson on CoddyKit — lesson 4 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 Supabase Backend as a Service learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Relationships Matter
Real data is connected: a user has many posts, an order has many items. Relationships link rows across tables so you avoid duplicating data.
What Is a Foreign Key?
A foreign key is a column that points to the primary key of another table. It enforces that the referenced row actually exists.
One-to-Many Example
One author writes many books. The books table stores the author's id.
CREATE TABLE authors (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name text NOT NULL
);
CREATE TABLE books (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
title text NOT NULL,
author_id bigint REFERENCES authors(id)
);Referential Integrity
Postgres will reject inserting a book with an author_id that does not exist. This keeps your data consistent.
ON DELETE Behavior
Decide what happens when a parent row is deleted:
CASCADEdeletes the children tooSET NULLclears the referenceRESTRICTblocks the delete
ALTER TABLE books
ADD CONSTRAINT fk_author
FOREIGN KEY (author_id)
REFERENCES authors(id)
ON DELETE CASCADE;Querying Across Tables
With the Supabase client you can fetch related rows in one call using nested selects.
const { data } = await supabase
.from('authors')
.select('name, books(title)');Many-to-Many Relationships
When both sides can have many of the other (students and courses), use a join table holding both foreign keys.
CREATE TABLE enrollments (
student_id bigint REFERENCES students(id),
course_id bigint REFERENCES courses(id),
PRIMARY KEY (student_id, course_id)
);Composite Primary Keys
In a join table the pair of foreign keys often forms the primary key, preventing duplicate links between the same two rows.
Naming Conventions
A common convention names foreign keys as singular_id (e.g. author_id). Consistency makes nested queries predictable.
function fkName(table) {
return table.replace(/s$/, '') + '_id';
}
console.log(fkName('authors'));Visualizing in the Dashboard
The Supabase Table Editor shows relationship links, and the Schema Visualizer draws how your tables connect, which helps verify your design.
Putting It Together
Model connections with foreign keys, choose sensible ON DELETE rules, and use join tables for many-to-many. This is the backbone of a clean schema.
Quick Check
Test your understanding of relationships.
Recap
You learned foreign keys, one-to-many vs many-to-many relationships, ON DELETE behaviors, join tables with composite keys, and how to query related data with nested selects.
Frequently asked questions
Is the “Relationships and Foreign Keys” lesson free?
Yes — the full text of “Relationships and Foreign Keys” is free to read here on the web, and the Supabase Backend as a Service 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 Supabase Backend as a Service course, upgrade to CoddyKit PRO.
What will I learn in “Relationships and Foreign Keys”?
Connect tables together using foreign keys to model one-to-many and many-to-many relationships in your Supabase Postgres database. You practise Supabase Backend as a Service 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 Supabase Backend as a Service?
No prior experience is required. Supabase Backend as a Service on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “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 Supabase Backend as a Service lesson?
Yes. Every Supabase Backend as a Service 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
- Designing Database Schemas
- Creating Tables and Columns
- Basic Data Insertion & Querying
- Relationships and Foreign Keys