Skip to content

Instantly share code, notes, and snippets.

@metalmatze
Created March 22, 2016 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metalmatze/c8a0509069142533780a to your computer and use it in GitHub Desktop.
Save metalmatze/c8a0509069142533780a to your computer and use it in GitHub Desktop.
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