Step-by-step guide to modeling relationships in Supabase. Learn to design schemas, create tables, and implement one-to-one, one-to-many, and many-to-many relations with SQL.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Setting Up Your Supabase Project
Step 2: Designing the Database Schema
Example entities and relationships:
Step 3: Creating Tables for Entities
Example for creating a "Users" table:
CREATE TABLE public.users (
id serial PRIMARY KEY,
username text UNIQUE NOT NULL,
email text UNIQUE NOT NULL
);
Example for creating a "Profiles" table:
CREATE TABLE public.profiles (
id serial PRIMARY KEY,
user_id integer REFERENCES public.users(id) UNIQUE,
bio text
);
Step 4: Modeling One-to-One Relationships
Example for a one-to-one relationship between "Users" and "Profiles":
ALTER TABLE public.profiles
ADD CONSTRAINT fk_user
FOREIGN KEY(user_id) REFERENCES public.users(id) UNIQUE;
Step 5: Modeling One-to-Many Relationships
Example for a one-to-many relationship between "Authors" and "Books":
CREATE TABLE public.books (
id serial PRIMARY KEY,
title text NOT NULL,
author_id integer REFERENCES public.authors(id)
);
**Step 6: Modeling Many-to-Many Relationships**
- For many-to-many relationships, use a junction table with foreign keys referencing each of the two tables to define the relationship.
Example for a many-to-many relationship between "Students" and "Courses":
- Create a "Students" table and a "Courses" table:
CREATE TABLE public.students (
id serial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE public.courses (
id serial PRIMARY KEY,
title text NOT NULL
);
- Create a junction table "Enrollments":
CREATE TABLE public.enrollments (
student_id integer REFERENCES public.students(id),
course_id integer REFERENCES public.courses(id),
PRIMARY KEY(student_id, course_id)
);
**Step 7: Using Supabase to Interact with the Database**
- Use Supabase client libraries (JavaScript, Python, etc.) to interact with the database.
JavaScript Example:
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'your-supabase-url';
const supabaseKey = 'your-supabase-key';
const supabase = createClient(supabaseUrl, supabaseKey);
async function fetchUsers() {
const { data, error } = await supabase
.from('users')
.select('*');
if (error) console.log('Error fetching users:', error);
else console.log('Users:', data);
}
**Step 8: Testing the Relationships**
- Perform insertions to ensure that relationships are established correctly.
Insert Example:
-- Inserting a user
INSERT INTO public.users (username, email) VALUES ('john_doe', 'john@example.com');
-- Inserting a profile linked to the user
INSERT INTO public.profiles (user_id, bio) VALUES (1, 'Software Developer');
- Validate the relationships using SQL JOIN queries to fetch related data.
Example of a JOIN query:
SELECT users.username, profiles.bio
FROM public.users
JOIN public.profiles ON users.id = profiles.user_id;
This comprehensive tutorial illustrates the steps to model relationships in Supabase, ensuring each part of the process is meticulously explained.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.