如何在gorm中查询列不为空的数据
这是我的两个模型,我想让我的代码返回其中有节点的所有路径,并排除其中没有节点的所有路径
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加额外的 INNER JOIN 以仅加载具有节点的路径。它看起来像这样:
You could add an additional INNER JOIN to load only paths that have nodes. It would look something like this: