/supabase-tutorials

How to add a foreign key in Supabase?

Learn how to add a foreign key in Supabase with our step-by-step guide. Navigate your dashboard, configure tables, and ensure relational integrity easily.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to add a foreign key in Supabase?

 

Step 1: Open the Supabase Dashboard

 

  • First, navigate to the Supabase website and log into your account.
  • Once logged in, open your project dashboard. If you have multiple projects, select the one where you want to add the foreign key.

 

Step 2: Access the Database Section

 

  • On the left-hand sidebar, locate and click on Database.
  • Under the database section, click on Tables to see the list of all available tables.

 

Step 3: Identify Tables for Foreign Key Relationship

 

  • Identify the table that will contain the foreign key constraint. This table must have a column that can act as a foreign key.
  • Also, identify the table that the foreign key will reference. This table should have a primary key that the foreign key will match.

 

Step 4: Open Table Editor

 

  • In the list of tables, click on the table that will contain the foreign key. This will open the table editor interface.

 

Step 5: Add a Foreign Key

 

  • Inside the table editor, locate the specific column that you want to use as a foreign key.
  • Navigate to the foreign key section within the column settings or constraints section.
  • Click on the option to Add Foreign Key.

 

Step 6: Configure the Foreign Key Settings

 

  • A dialog will appear for foreign key configuration.
  • Select the referenced table and the column that will be the target of the foreign key relationship.
  • Configure additional settings if necessary, such as ON DELETE or ON UPDATE actions, which define the behavior when the referenced data is deleted or updated.

 

Step 7: Save Changes

 

  • Once you have configured the foreign key, click the Save button to apply the changes.
  • Ensure that there are no conflicts or errors in your configuration. If there are existing discrepancies, Supabase will notify you.

 

Step 8: Verify the Foreign Key

 

  • After saving, return to the table view to check the overview of columns and constraints.
  • Ensure the foreign key is listed under constraints, confirming that it has been successfully added.

 

By following these steps, you can effectively add a foreign key to a table in Supabase, ensuring relational integrity between your database tables.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022