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.