Course lesson

Setup a Foreign Key relationship between PostgreSQL tables

Supabase uses the auth.users table to store information about our users and their sessions. In this lesson, we add a column to the tweets table to associate each tweet with one of our users.

Duration
4 min
Access
Free
Transcript
Retained from source evidence

Supabase uses the auth.users table to store information about our users and their sessions. In this lesson, we add a column to the tweets table to associate each tweet with one of our users.

Additionally, we set up cascading deletes, so when a user is deleted from the auth.users table, their tweets are also deleted.

Lastly, because our database schema has changed, our TypeScript definitions are now incorrect. Therefore, we use the Supabase CLI to introspect our PostgreSQL schema and create a new database.types.ts file.

Code Snippets

Add foreign key relationship to auth.users

alter table public.tweets
add user_id uuid references auth.users on delete cascade not null;

Generate TypeScript definitions

Terminal
npx supabase gen types typescript --project-id your-project-id > lib/database.types.ts

Resources