How can I query a one-to-one relationship with foreign key in supabase?
My tables:
- games (id, created_at, played_at)
- results (game_id, key)
Data: games
("id": 1, "created_at": "2022-03-02T11:01:11+00:00", "played_at": "2022-04-02T12:15:06+00:00")
Data: results
("game_id": 1, "key": "alyktm3cf11bypgg")
My supabase query looks like this:
const { data, error } = await supabase
.from('games')
.select('*, registrations(walletKey), results(key)')
.order('scheduled_at')
;
The result contains an array of registrations, like here:
{
"id": 1,
"created_at": "2022-03-02T11:01:11+00:00",
"played_at": "2022-04-02T12:15:06+00:00",
"results": [
{
"key": "alyktm3cf11bypgg"
}
]
}
But since there will always only be one result, can I modify my query to get this?
{
"id": 1,
"created_at": "2022-03-02T11:01:11+00:00",
"played_at": "2022-04-02T12:15:06+00:00",
"results":
{
"key": "alyktm3cf11bypgg"
}
}
Or even this:
{
"id": 1,
"created_at": "2022-03-02T11:01:11+00:00",
"played_at": "2022-04-02T12:15:06+00:00",
"key": "alyktm3cf11bypgg"
}
const newDataObj = Object.fromEntries(Object.entries(oldDataObj).map(([k, v]) => (k !== 'results' ? [k, v] : ["key", v?.[0]?.key])));
. I assume the objective is not this - but rather get the result from directly in the format.