I want to work on a table many2many generated automatically by GORM db.AutoMigrate(&Document{}, &Folder{}).
In this scenario, a folder has documents and documents have folders, so far it's good, but the association has a status (valid, invalid ...) so I have to look at the documents_folders table to know the state.
Database:
+-----------------------+
| Table |
+-----------------------+
| documents |
| documents_folders |
| folders |
+-----------------------+
Models :
Stuff
(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"`
}
Folder
type Folder struct {
Stuff
Name string `json:"name" form:"name" gorm:"name;"`
Documents []*Document `json:"documents" gorm:"many2many:documents_folders;"`
}
Document
type Document struct {
Stuff
Name string `json:"name" form:"name" gorm:"name;"`
Folders []*Folder `json:"folders" gorm:"many2many:documents_folders;"`
}
DocumentsFolders
I created this model to access the table, it is generated automatically but I have to add the column State
type DocumentsFolders struct {
DocumentID string `json:"document_id" sql:"type:char(36) REFERENCES documents(id)"`
Document *Document `json:"document"`
FolderID string `json:"folder_id" sql:"type:char(36) REFERENCES folders(id)"`
Folder *Folder `json:"folder"`
State string `json:"state" form:"state" gorm:"state;"`
}
I can not access it when I preload the document and folder like :
folder := Folder{}
db.Preload("Documents").Where("id = ?", "myUUID").First(&folder)
I would like to see the State of the document association.
Thanks for your help