0
0
Supabasecloud~20 mins

Table relationships in Supabase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Table Relationships Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Foreign Key Constraints in Supabase

In Supabase, you want to link two tables: orders and customers. Each order must belong to one customer. Which of the following best describes the role of a foreign key in this relationship?

AA foreign key in <code>orders</code> points to a non-unique column in <code>customers</code>, allowing multiple customers per order.
BA foreign key in <code>customers</code> points to the primary key in <code>orders</code>, ensuring each customer has an order.
CA foreign key in <code>orders</code> points to the primary key in <code>customers</code>, ensuring each order is linked to a valid customer.
DA foreign key in <code>customers</code> points to a foreign key in <code>orders</code>, creating a circular reference.
Attempts:
2 left
💡 Hint

Think about which table holds the reference to the other to enforce the link.

Configuration
intermediate
2:00remaining
Configuring One-to-Many Relationship in Supabase

You have two tables: authors and books. Each author can write many books. Which SQL snippet correctly creates a foreign key in books to link to authors?

AALTER TABLE books ADD COLUMN author_id UUID REFERENCES authors(id);
BALTER TABLE authors ADD COLUMN book_id UUID REFERENCES books(id);
CALTER TABLE books ADD COLUMN author_id UUID UNIQUE REFERENCES authors(id);
DALTER TABLE books ADD COLUMN author_id UUID NOT NULL;
Attempts:
2 left
💡 Hint

The foreign key should be in the table that holds many records.

Architecture
advanced
2:30remaining
Designing Many-to-Many Relationships in Supabase

You want to model a many-to-many relationship between students and courses in Supabase. Which architecture is the best practice?

AAdd a foreign key column in <code>courses</code> pointing to <code>students</code>.
BCreate a join table <code>enrollments</code> with foreign keys to both <code>students</code> and <code>courses</code>.
CAdd a foreign key column in <code>students</code> pointing to <code>courses</code>.
DStore course IDs as a list inside a column in <code>students</code>.
Attempts:
2 left
💡 Hint

Many-to-many relationships usually require a separate table to link records.

security
advanced
2:30remaining
Securing Table Relationships with Row Level Security (RLS)

In Supabase, you want to ensure users can only see their own orders. The orders table has a foreign key user_id referencing users. Which RLS policy correctly enforces this?

ACREATE POLICY "Users can view their orders" ON orders FOR SELECT USING (user_id = auth.uid());
BCREATE POLICY "Users can view all orders" ON orders FOR SELECT USING (true);
CCREATE POLICY "Users can view orders" ON orders FOR SELECT USING (user_id != auth.uid());
DCREATE POLICY "Users can view orders" ON orders FOR SELECT USING (user_id IS NULL);
Attempts:
2 left
💡 Hint

Check the condition that matches the current user's ID with the order's user_id.

service_behavior
expert
3:00remaining
Behavior of Cascading Deletes in Supabase Table Relationships

You have two tables: projects and tasks. tasks has a foreign key project_id referencing projects with ON DELETE CASCADE. What happens when you delete a project?

AThe delete fails if there are tasks linked to the project.
BOnly the project is deleted; tasks remain unchanged.
CThe tasks remain but their <code>project_id</code> is set to NULL.
DAll tasks linked to the deleted project are automatically deleted.
Attempts:
2 left
💡 Hint

Consider what ON DELETE CASCADE means for related records.