# Foreign Key relation good practices

**URL:** <https://community.weweb.io/t/foreign-key-relation-good-practices/8336>\
**Category:** Tips and Hacks\
**Created:** [April 27, 2024, 11:43am UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336 "2024-04-27T11:43:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Corner](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Corner](https://community.weweb.io/u/Corner)\
**Post date:** [April 27, 2024, 11:43am UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/1 "2024-04-27T11:43:33Z")

</div>

Hello there, I was wondering if there are some good practices I should know regarding fk relations (like setting up a fk relation only between uuid, not text ? Or only with primary keys ?) ? This maybe sounds a bit random but I’m facing some strange problems here, so maybe I missed something.  
Thanks in advance !  
Pierre-Louis

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [April 28, 2024, 7:46am UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/2 "2024-04-28T07:46:00Z")

</div>

Hey Pierre, it might be cool if you told us a few more things about your enquiry such as:

1. What kind of backend you’re using?
2. Wat strange problems you’re facing?
3. Some more concrete examples would help a lot

Generally, a foreign key can be anything, usually though it’s id’s which come in a `uuid` or `bigint (int8)` type. You could still do things like text, but it’s rather unusual, as the text can have some caveats, such as for example you can have some special characters that could cause problems etc. It really depends on the usecase.

---

<div class="post-metadata">

**Author:** ![Corner](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Corner](https://community.weweb.io/u/Corner)\
**Post date:** [April 29, 2024, 10:14am UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/3 "2024-04-29T10:14:30Z")

</div>

Hey @Broberto, thanks for the reply.

I’m using Supabase as a backend, and as you can see, I’m learning how to use it. I now manage to set up foreign keys (my strange problem was that nothing happened when saving my fk relation after setting it up, but this was because I didn’t set the unique parameter in the foreign column, and the browser I’m using (Brave) didn’t show me an error message…), but I feel that doing a fk relation with a text is not really something to do, and you just confirmed it !  
To finish this topic, are we thus supposed to set up fk only with uuid or bigint, and then make a lookup within the frontend to retrieve and display some specific data ?

What I’m still struggling though is to understand the difference between setting up a one to one relationship and a one to many relationship in Supabase : it looks to be truly similar ! I don’t know what are your skills on Supabase, maybe this is not the place to talk about this

---

<div class="post-metadata">

**Author:** ![carrano\_dot\_dev](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/carrano_dot_dev/32/10547_2.png) [@carrano\_dot\_dev](https://community.weweb.io/u/carrano_dot_dev)\
**Post date:** [April 29, 2024, 10:55am UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/4 "2024-04-29T10:55:17Z")

</div>

[This should help you](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-foreign-key/)

Basically, a fk should reference a unique field in a table, typically the primary key.

You then use [JOINs to query the data](https://www.postgresql.org/docs/current/tutorial-join.html).

---

<div class="post-metadata">

**Author:** ![Corner](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Corner](https://community.weweb.io/u/Corner)\
**Post date:** [April 29, 2024, 12:26pm UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/5 "2024-04-29T12:26:21Z")

</div>

Okay very clear thank you @carrano_dot_dev

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [July 21, 2024, 12:30pm UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/6 "2024-07-21T12:30:10Z")

</div>

Hey, I just came back to this topic, to say that If anyone would like to do JOINS directly from the WeWeb instead of writing them in the backend and fetching them from there, you can try using this guide to set it up, it talks about how to fetch data by using the Advanced tab in the WeWeb’s Supabase Plugin, you can achieve a lot with this.

> **[Become a Supabase Magician in WeWeb (With Examples) – Broberto](https://broberto.sk/weweb/become-a-supabase-magician-in-weweb-with-examples/)**
>
> Discover advanced techniques for using Supabase with WeWeb. Enhance your apps with advanced tips and interactive WeWeb examples.

---

<div class="post-metadata">

**Author:** ![Corner](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Corner](https://community.weweb.io/u/Corner)\
**Post date:** [July 22, 2024, 5:00pm UTC](https://community.weweb.io/t/foreign-key-relation-good-practices/8336/7 "2024-07-22T17:00:13Z")

</div>

thanks @Broberto, very handy 🙂
