Foreign Key relation good practices

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

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.

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

This should help you

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

You then use JOINs to query the data.

1 Like

Okay very clear thank you @carrano_dot_dev