我正在尝试用GORM提出具体要求.
这是我使用的表格:
+-------------------+
| Tables |
+-------------------+
| locations |
| shops |
| shops_tags |
| tags |
+-------------------+
Run Code Online (Sandbox Code Playgroud)
地点表
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| shop_id | bigint(20) | YES | | NULL | |
| lat | decimal(10,8) | YES | | NULL | |
| lng | decimal(11,8) | YES …Run Code Online (Sandbox Code Playgroud) 我想处理many2many由 GORM 自动生成的表db.AutoMigrate(&Document{}, &Folder{})。
在这种情况下,文件夹有文档,文档有文件夹,到目前为止还不错,但是关联有状态(有效、无效...),所以我必须查看documents_folders表格才能知道状态。
+-----------------------+
| Table |
+-----------------------+
| documents |
| documents_folders |
| folders |
+-----------------------+
Run Code Online (Sandbox Code Playgroud)
(ID primary_key == UUID)
type Stuff struct {
ID uuid.UUID `gorm:"type:char(36);primary_key;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"update_at"`
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
}
Run Code Online (Sandbox Code Playgroud)
type Folder struct {
Stuff
Name string `json:"name" form:"name" gorm:"name;"`
Documents []*Document `json:"documents" gorm:"many2many:documents_folders;"`
}
Run Code Online (Sandbox Code Playgroud)
type Document struct {
Stuff
Name string `json:"name" form:"name" gorm:"name;"` …Run Code Online (Sandbox Code Playgroud)