Skip to content

Instantly share code, notes, and snippets.

@metalmatze metalmatze/gorm-join.go Secret
Created Mar 22, 2016

Embed
What would you like to do?
Scan gorm join resulsts with nested objects
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
You can’t perform that action at this time.