How to use a triple join in supabase? #1509
-
Hello, I am kinda stuck in a architectural and understanding problem. I have three tables: players, matches and heroes. And that works fine, at least I am getting the data I want. But here's the thing: I am getting them duplicated as soon as I have at least two matching columns. This is how I query my data:
For example do I have two rows in my table:
This would result in a output like this:
Everythings fine, just as I want it.
I am getting something like this:
I assume whats causing this is the fact that theres some kind of missing uniqueness to the rows, which is totally intended. This is my first project with Supabase and I'm not really experienced with JS development in general, so any tips would be really appreciated. Best, Niklas |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi there @jarckn, I think your model could benefit from a bit of normalization, that way you can avoid duplicates. It will require one more join table. So IIUC, a player can pick one or more heroes for the match. Then we could have the model like this:
A
Then you could do that query like this: let { data, error } = await supabase
.from('matches')
.select('*, plays(player:players(*),heroes(*))'); Click to see result[ { "id": 4, "name": "match-4", "plays": [ { "player": { "id": 3, "name": "player-3" }, "heroes": [ { "id": 5, "name": "hero-5" }, { "id": 6, "name": "hero-6" } ] }, { "player": { "id": 4, "name": "player-4" }, "heroes": [ { "id": 6, "name": "hero-6" } ] } ] } ] Hope that helps. This is the SQL I've used for the example: Sample SQLcreate table matches(id int primary key, name text); create table players(id int primary key, name text); create table heroes(id int primary key, name text); |
Beta Was this translation helpful? Give feedback.
Hi there @jarckn,
I think your model could benefit from a bit of normalization, that way you can avoid duplicates. It will require one more join table.
So IIUC, a player can pick one or more heroes for the match. Then we could have the model like this:
A
plays
join table for the M2M relationship betweenplayers
andmatches
, and apicks
join table for the M2M onplays
andheroes
.Then you could do that query like this:
Click to see result