I'm trying to get the name of platforms based on games id.
I have three tables as below and I'm trying to JOIN them to get result that needed:
Games
Id | .....| .....|
---|------ ------|
1 | . | . |
2 | . | . |
3 | . | . |
4 | . | . |
Game_Platforms
Id |....|game_id| platform_id|...|
---------------------------------
1 | . | 1 | 1 |.. |
2 | . | 1 | 2 |.. |
3 | . | 3 | 3 |.. |
.. | . | 4 | 4 |.. |
Platforms
Id| ...|...| name |
---------------------|
1 | . | . | iOS |
2 | . | . | Android |
3 | . | . | Windows |
4 | . | . | SteamOS |
type Platforms struct {
Name string
}
var name []Platforms
query = postgres.Db().Select("name").
Joins("JOIN games ON games.id = game_platforms.game_id").
Joins("JOIN game_platforms ON game_platforms.platform_id = platforms.id").
Where("games.id = 1").Find(&name)
I expect to get the Platforms name, but get the error:
pq: missing FROM-clause entry for table "game_platforms"
I think that I wrote Joins commands incorrect, but it seems logical, maybe I'm wrong.
So answer to my question wasn't without of support of @novalagung
Credit to him, so i need to change query to look like this
query = postgres.Db().Select("name").
Joins("JOIN game_platforms ON game_platforms.platform_id = platforms.id").
Joins("JOIN games ON games.id = game_platforms.game_id").
Where("games.id = 1").Find(&name)
However I had an error that said pq: column reference "name" is ambiguous
But it is my fault because I didn't specified in question all tables, so small changes need to be done to solve this error is Select("platforms.name").
You query is missing the FROM
clause.
I assume you wanted to select the data from platforms
table. If it is, then (as per your code) you shall join with the game_platforms
first then with the games
.
query = postgres.Db().Select("name").
Find("platforms"). // <------ this one
Joins("JOIN game_platforms ON game_platforms.platform_id = platforms.id").
Joins("JOIN games ON games.id = game_platforms.game_id").
Where("games.id = 1").Find(&name)