'What other attribute should i add for my Gorm table?
My goal is to simply join three tables (Sorry my knowledge in sql is limited)
I'm building a community-based platform like Patreon where each community can have different plans (Free, Paid)
Example Code:
type User struct {
Communities []Community `gorm:"many2many:user_communities" json:"communities"`
}
type Community struct {
UserID `json:"userID"`
Users []User `gorm:"many2many:user_communities" json:"users"`
Plans []Plan `json:"plans"`
// How do i add another attirbute to check for user with different plan?
}
type Plan struct {
CommunityID uint `json:"communityID"`
Name string `json:"name"`
Price string `json:"price"`
}
Based on the model above
- One user can join many communities
- One community can have many users
- One user can own a community (creator)
- and for each community, one user can join for the different plans (Free, Paid)
I'm more focused on how to solve number 4
So the question here is what should I add for my community table to differentiate between the user who joins as Free plan or Paid plan?
because I can only query users who belong to a specific community not users who belong to that community based on a Free or Paid plan.
Example query
// Find whether that user belongs to a community
if err := database.Mysql.Table("users u").Joins("JOIN user_communities uc ON u.id = uc.user_id").Where("u.id = ? AND uc.community_id = ?", userID, communityID).Select("1").Scan(&found).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "error happened"})
return
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
