Created
November 27, 2020 16:06
-
-
Save dipeshhkc/8345662c3fe42d566b86b5d67a2d3ef5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//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