Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Created November 27, 2020 16:06
Show Gist options
  • Save dipeshhkc/8345662c3fe42d566b86b5d67a2d3ef5 to your computer and use it in GitHub Desktop.
Save dipeshhkc/8345662c3fe42d566b86b5d67a2d3ef5 to your computer and use it in GitHub Desktop.
//Create New POST
post := &Post{Name: "Post2"}
if db.Create(&post).Error != nil {
log.Panic("Unable to create post")
}
//RELATION QUERY (POST & COMMENT)
comment := &Comment{Name: "Nice Post", PostID: post.ID}
if db.Create(&comment).Error != nil {
log.Panic("Unable to create comment")
}
//WHERE
fetchedPost := &Post{}
if err := db.Where("id = ?", comment.PostID).Preload("Comment").First(&fetchedPost).Error; err != nil {
log.Panic("Unable to find created Post.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment