访问许多表数据

时间:2019-10-04 10:12:09

标签: go go-gorm

我想处理由GORM many2many自动生成的表db.AutoMigrate(&Document{}, &Folder{})

在这种情况下,文件夹中有文档,而文档中也有文件夹,到目前为止还不错,但是关联具有状态(有效,无效...),因此我必须查看documents_folders表才能知道状态。

数据库:

+-----------------------+
| Table                 |
+-----------------------+
| documents             |
| documents_folders     |
| folders               |
+-----------------------+

模型:

东西

(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"`
}

文件夹

type Folder struct {
    Stuff
    Name      string      `json:"name" form:"name" gorm:"name;"`
    Documents []*Document `json:"documents" gorm:"many2many:documents_folders;"`
}

文档

type Document struct {
    Stuff
    Name        string    `json:"name" form:"name" gorm:"name;"`
    Folders     []*Folder `json:"folders" gorm:"many2many:documents_folders;"`
}

DocumentsFolders

我创建了这个模型来访问表,它是自动生成的,但是我必须添加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;"`
}

当我像这样预加载documentfolder时,我无法访问它:

folder := Folder{}
db.Preload("Documents").Where("id = ?", "myUUID").First(&folder)

我想查看文档关联的状态。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您遇到以下问题:

  • 您错过了为AutoMigrate添加DocumentsFolders的情况。
  • 不要将切片与指针一起使用。

检查以下固定示例以获取更多参考:

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "math/rand"
    "time"
)

type UUID = string

type Model struct {
    ID        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"`
}

type Folder struct {
    Model
    Name      string     `json:"name" form:"name" gorm:"name;"`
    Documents []Document `json:"documents" gorm:"many2many:documents_folders;"`
}

type Document struct {
    Model
    Name    string   `json:"name" form:"name" gorm:"name;"`
    Folders []Folder `json:"folders" gorm:"many2many:documents_folders;"`
}

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;"`
}

var i = rand.Int()

func genId() string {
    id := i
    i++
    return fmt.Sprintf("%036d", id)
}

func main() {
    db, _ := gorm.Open("sqlite3", "./db.sqlite")
    db.AutoMigrate(&Document{}, &Folder{}, &DocumentsFolders{})

    folder1 := &Folder{
        Model:     Model{ID: genId()},
        Name:      "myFolder",
        Documents: nil,
    }
    db.FirstOrCreate(&folder1, folder1.ID)

    doc1 := &Document{
        Model:   Model{ID: genId()},
        Name:    "doc1",
    }
    db.FirstOrCreate(doc1, doc1.ID)

    doc2 := &Document{
        Model:   Model{ID: genId()},
        Name:    "doc2",
    }
    db.FirstOrCreate(doc2, doc2.ID)

    // join them
    db.Create(&DocumentsFolders{
        DocumentID: doc1.ID,
        FolderID:   folder1.ID,
        State:      "happy!",
    })
    db.Create(&DocumentsFolders{
        DocumentID: doc2.ID,
        FolderID:   folder1.ID,
        State:      "happy!",
    })

    // get the new folder to check
    db.Preload("Documents").First(folder1, folder1.ID)

    // two documents are printed
    fmt.Printf("Documents count: %d\n", len(folder1.Documents)) // prints 2
}