gin framework of go language + mysql to do web development, the association between models, here is an example:
package model
import "time"
// Article
type Article struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `sql:"index" json:"deletedAt"`
Name string `json:"name"`
BrowseCount uint `json:"browseCount"`
CommentCount uint `json:"commentCount"`
CollectCount uint `json:"collectCount"`
Status int `json:"status"`
Content string `json:"content"`
HTMLContent string `json:"htmlContent"`
ContentType int `json:"contentType"`
Categories []Category `gorm:"many2many:article_category;ForeignKey:ID;AssociationForeignKey:ID" json:"categories"`
Comments []Comment `gorm:"ForeignKey:SourceID" json:"comments"`
UserID uint `json:"userID"`
User User `json:"user"`
LastUserID uint `json:"lastUserID"` //
LastUser User `json:"lastUser"`
LastCommentAt *time.Time `json:"lastCommentAt"`
}
question:
1, User and Article have an one-to-many relationship. I don"t quite understand why the above code should write User
and UserID
, just UserID
.
2. Write User
here, meaning that the articles
table has a user
field to hold the information about this user?
add:
the sql code for creating the table does not write the user field, so why should the above Article structure be written with User??