使用GORM预加载过滤数据库行

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

Filter db rows with GORM preload

问题

目前,我得到了这个结构体:

type Token struct {
	gorm.Model
	Name          string              `gorm:"column:name"`
	Enabled       bool                `gorm:"column:enabled"`
	Symbol        string              `gorm:"column:symbol"`
	TokenDetails  []*EarnTokenDetail  `gorm:"foreignkey:TokenID;references:ID"`
}

type EarnTokenDetail struct {
	gorm.Model
	Name        string       `gorm:"column:name"`
	TokenID     uint64       `gorm:"column:token_id"`
	Enabled     bool         `gorm:"column:enabled"`
	Chains      *EarnChain   `gorm:"foreignkey:ID;references:ChainID"`
}

type EarnChain struct {
	ID        uint64    `gorm:"primary_key column:id"`
	Enabled   bool      `gorm:"column:enabled"`
	Name      string    `gorm:"column:name"`
}

以及这个 GORM 查询:

var tokens []*model.Token
result := e.db.
	WithContext(ctx).
	Preload("TokenDetails", "token_details.enabled = true").
	Preload("TokenDetails.Chains", "chains.enabled = true").
	Find(&tokens, "tokens.enabled = true")

当数据库中禁用了链条时,它在一切启用的情况下工作得很好,但结果仍然会显示具有禁用链条的令牌,并且Chains字段为空。

在仍然使用预加载的情况下,如何过滤掉这些行呢?

英文:

Currently, I got this struct

type Token struct {
	gorm.Model
	Name             string `gorm:"column:name"`
	Enabled          bool   `gorm:"column:enabled"`
	Symbol           string `gorm:"column:symbol"`
	TokenDetails    []*EarnTokenDetail   `gorm:"foreignkey:TokenID;references:ID"`
}

type EarnTokenDetail struct {
	gorm.Model
	Name               string     `gorm:"column:name"`
	TokenID            uint64     `gorm:"column:token_id"`
	Enabled          bool   `gorm:"column:enabled"`
	Chains             *EarnChain `gorm:"foreignkey:ID;references:ChainID"`
}

type EarnChain struct {
	ID        uint64    `gorm:"primary_key column:id"`
	Enabled          bool   `gorm:"column:enabled"`
	Name      string    `gorm:"column:name"`
}

And this GORM query:

	var tokens []*model.Token
	result := e.db.
		WithContext(ctx).
		Preload("TokenDetails", "token_details.enabled = true").
		Preload("TokenDetails.Chains", "chains.enabled = true").
		Find(&tokens, "tokens.enabled = true")

It works fine when everything is enabled, but when I disable chains in the database, the result will still show the tokens with disabled chains, with the Chains field empty.

How can I filter out those rows while still using preload?

答案1

得分: 1

根据GORM文档,这是预期的行为。它会一个接一个地执行查询,所以没有办法“丢弃”前面查询的结果。如果你根本不想要这些结果,即使是空的Chain字段,可以考虑使用Join()来过滤掉它们。我甚至找到了一个指出这一点的评论:https://stackoverflow.com/questions/51244274/how-to-multiple-table-joins-in-gorm#comment123086923_52949659

英文:

According the the GORM documentation, that's the expected behavior. It does one query after another, so there is no way to "drop" results of the former queries. If you don't want these results at all - not even with an empty Chain field, consider to use Join() to filter out those. I think I even found a comment pointing that out: https://stackoverflow.com/questions/51244274/how-to-multiple-table-joins-in-gorm#comment123086923_52949659

huangapple
  • 本文由 发表于 2022年8月1日 09:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73188103.html
匿名

发表评论

匿名网友

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

确定