如何在gorm中查询列不为空的数据

发布于 2025-01-12 22:30:16 字数 1898 浏览 1 评论 0原文

这是我的两个模型,我想让我的代码返回其中有节点的所有路径,并排除其中没有节点的所有路径

type Path struct {
    gorm.Model
    ID          uuid.UUID `json:"id" gorm:"type:uuid;primary_key"`
    Name        string    `json:"name" gorm:"type:varchar(255)"`
    Description string    `json:"description" gorm:"type:varchar(255)"`
    UserID      uuid.UUID `json:"user_id" gorm:"type:uuid"`
    Owner       User      `json:"owner" gorm:"foreignKey:UserID"`
    Nodes       []Node    `gorm:"foreignKey:PathID"`
}

type Node struct {
    gorm.Model
    ID          uuid.UUID `json:"_id" gorm:"type:uuid;primary_key"`
    Name        string    `json:"name" gorm:"type:varchar(255)"`
    Description string    `json:"description" gorm:"type:varchar(255)"`
    Url         string    `json:"url" gorm:"required"`
    Nodetype    string    `json:"nodetype" gorm:"type:varchar(255)"`
    PathID      uuid.UUID `json:"path_id" gorm:"type:uuid"`
    Path        Path      `gorm:"foreignKey:PathID"`
}

func (path *Path) BeforeCreate(tx *gorm.DB) (err error) {
    // UUID version 4
    path.ID = uuid.New()
    return
}

func (node *Node) BeforeCreate(tx *gorm.DB) (err error) {
    // UUID version 4
    node.ID = uuid.New()
    return
}

我想获取

我尝试过这种方法的节点的所有可用路径,但是不起作用

func GetAllPaths(c *fiber.Ctx) error {
    db := database.DB

    paths := []models.Path{}

    err := db.Debug().Joins("Owner").Preload("Nodes").Where("nodes IS NOT NULL").Find(&paths).Error
    if err != nil {
        return c.Status(500).SendString(err.Error())
    }

    var allPaths []serializer.PathResponseStruct

    for _, path := range paths {
        rpath := serializer.PathResponse(path)
        allPaths = append(allPaths, rpath)
    }

    return c.Status(200).JSON(allPaths)
}

我想要的响应是带有节点的数组路径,而不是空的节点数组(空)

Here is my two models, I want to have my code return all the paths which has nodes inside them, and exclude all the path which has no nodes inside them

type Path struct {
    gorm.Model
    ID          uuid.UUID `json:"id" gorm:"type:uuid;primary_key"`
    Name        string    `json:"name" gorm:"type:varchar(255)"`
    Description string    `json:"description" gorm:"type:varchar(255)"`
    UserID      uuid.UUID `json:"user_id" gorm:"type:uuid"`
    Owner       User      `json:"owner" gorm:"foreignKey:UserID"`
    Nodes       []Node    `gorm:"foreignKey:PathID"`
}

type Node struct {
    gorm.Model
    ID          uuid.UUID `json:"_id" gorm:"type:uuid;primary_key"`
    Name        string    `json:"name" gorm:"type:varchar(255)"`
    Description string    `json:"description" gorm:"type:varchar(255)"`
    Url         string    `json:"url" gorm:"required"`
    Nodetype    string    `json:"nodetype" gorm:"type:varchar(255)"`
    PathID      uuid.UUID `json:"path_id" gorm:"type:uuid"`
    Path        Path      `gorm:"foreignKey:PathID"`
}

func (path *Path) BeforeCreate(tx *gorm.DB) (err error) {
    // UUID version 4
    path.ID = uuid.New()
    return
}

func (node *Node) BeforeCreate(tx *gorm.DB) (err error) {
    // UUID version 4
    node.ID = uuid.New()
    return
}

I want to fetch all the paths available with nodes

I have tried with this approach but not working

func GetAllPaths(c *fiber.Ctx) error {
    db := database.DB

    paths := []models.Path{}

    err := db.Debug().Joins("Owner").Preload("Nodes").Where("nodes IS NOT NULL").Find(&paths).Error
    if err != nil {
        return c.Status(500).SendString(err.Error())
    }

    var allPaths []serializer.PathResponseStruct

    for _, path := range paths {
        rpath := serializer.PathResponse(path)
        allPaths = append(allPaths, rpath)
    }

    return c.Status(200).JSON(allPaths)
}

The response I want is array paths with nodes, not empty array of nodes (null)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画离情绘悲伤 2025-01-19 22:30:16

您可以添加额外的 INNER JOIN 以仅加载具有节点的路径。它看起来像这样:

paths := []models.Path{}
err := db.Debug().Preload("Owner").Preload("Nodes").    //if you want to load the Path inside the node, then it should be .Preload("Nodes.Path")
          Joins("INNER JOIN nodes ON nodes.path_id = paths.id").Find(&paths).Error

You could add an additional INNER JOIN to load only paths that have nodes. It would look something like this:

paths := []models.Path{}
err := db.Debug().Preload("Owner").Preload("Nodes").    //if you want to load the Path inside the node, then it should be .Preload("Nodes.Path")
          Joins("INNER JOIN nodes ON nodes.path_id = paths.id").Find(&paths).Error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文