将两个gorm查询合并为一个。

huangapple go评论96阅读模式
英文:

Turn two gorm queries into one

问题

我有以下模型:

type Instance struct {
    gorm.Model
    Name    string `gorm:"index:idx_name_and_group,unique"`
    UserID  uint
    GroupID uint `gorm:"index:idx_name_and_group,unique"`
    ...
}

type Group struct {
    gorm.Model
    Name       string `gorm:"unique;"`
    Instances  []Instance
    ...
}

我想通过名称和组名获取一个实例。

我可以使用以下代码来实现:

func (r instanceRepository) FindByName(groupName string, instanceName string) (*model.Instance, error) {
    var instance *model.Instance
    var group *model.Group

    err := r.db.
        First(&group, "name = ?", groupName).Error
    if err != nil {
        return nil, err
    }

    err = r.db.
        Where("name = ? and group_id = ?", instanceName, group.ID).
        First(&instance).Error

    return instance, err
}

但我想将其转换为一个查询。有什么想法如何实现?

英文:

I have the following models

type Instance struct {
	gorm.Model
	Name               string `gorm:"index:idx_name_and_group,unique"`
	UserID             uint
	GroupID            uint `gorm:"index:idx_name_and_group,unique"`
    ...
}

type Group struct {
	gorm.Model
	Name                 string `gorm:"unique;"`
	Instances            []Instance
    ...
}

I'm trying to get an instance by name and group name.

I can do it using the following code

func (r instanceRepository) FindByName(groupName string, instanceName string) (*model.Instance, error) {
	var instance *model.Instance
	var group *model.Group

	err := r.db.
		First(&group, "name = ?", groupName).Error
	if err != nil {
		return nil, err
	}

	err = r.db.
		Where("name = ? and group_id = ?", instanceName, group.ID).
		First(&instance).Error

	return instance, err
}

But I'd like to turn it into one query. Any ideas about how to achieve that?

答案1

得分: 0

你可以始终使用Joins函数来进行SQL连接操作。类似这样:

func (r instanceRepository) FindByName(groupName string, instanceName string) (*model.Instance, error) {
    var instance *model.Instance

    err := r.db.
        Joins("INNER JOIN groups g ON g.id = instances.group_id").
        Where("g.name = ? AND instances.name = ?", groupName, instanceName).
        First(&instance).Error
    if err != nil {
        return nil, err
    }

    return instance, err
}
英文:

You can always use the Joins function to do SQL joins. Something like this:

func (r instanceRepository) FindByName(groupName string, instanceName string) (*model.Instance, error) {
    var instance *model.Instance

    err := r.db.
        Joins("INNER JOIN groups g ON g.id = instances.group_id").
        Where("g.name = ? AND instances.name = ?", groupName, instanceName).
        First(&instance).Error
    if err != nil {
        return nil, err
    }

    return instance, err
}

huangapple
  • 本文由 发表于 2021年8月21日 00:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/68865225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定