-
-
Save metalmatze/c8a0509069142533780a to your computer and use it in GitHub Desktop.
Scan gorm join resulsts with nested objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Post struct { | |
ID int | |
Title string | |
Date time.Time | |
} | |
type User struct { | |
Name string | |
Post Post | |
} | |
func query() { | |
var users []User | |
gorm.Table("users"). | |
Select("users.name, post.id, post.title"). | |
Joins(`LEFT JOIN posts AS post | |
ON id = ( | |
SELECT id FROM posts WHERE user_id = id ORDER BY date DESC LIMIT 1 | |
)`). | |
Scan(&users) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment