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

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

Filter db rows with GORM preload

问题

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

  1. type Token struct {
  2. gorm.Model
  3. Name string `gorm:"column:name"`
  4. Enabled bool `gorm:"column:enabled"`
  5. Symbol string `gorm:"column:symbol"`
  6. TokenDetails []*EarnTokenDetail `gorm:"foreignkey:TokenID;references:ID"`
  7. }
  8. type EarnTokenDetail struct {
  9. gorm.Model
  10. Name string `gorm:"column:name"`
  11. TokenID uint64 `gorm:"column:token_id"`
  12. Enabled bool `gorm:"column:enabled"`
  13. Chains *EarnChain `gorm:"foreignkey:ID;references:ChainID"`
  14. }
  15. type EarnChain struct {
  16. ID uint64 `gorm:"primary_key column:id"`
  17. Enabled bool `gorm:"column:enabled"`
  18. Name string `gorm:"column:name"`
  19. }

以及这个 GORM 查询:

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

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

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

英文:

Currently, I got this struct

  1. type Token struct {
  2. gorm.Model
  3. Name string `gorm:"column:name"`
  4. Enabled bool `gorm:"column:enabled"`
  5. Symbol string `gorm:"column:symbol"`
  6. TokenDetails []*EarnTokenDetail `gorm:"foreignkey:TokenID;references:ID"`
  7. }
  8. type EarnTokenDetail struct {
  9. gorm.Model
  10. Name string `gorm:"column:name"`
  11. TokenID uint64 `gorm:"column:token_id"`
  12. Enabled bool `gorm:"column:enabled"`
  13. Chains *EarnChain `gorm:"foreignkey:ID;references:ChainID"`
  14. }
  15. type EarnChain struct {
  16. ID uint64 `gorm:"primary_key column:id"`
  17. Enabled bool `gorm:"column:enabled"`
  18. Name string `gorm:"column:name"`
  19. }

And this GORM query:

  1. var tokens []*model.Token
  2. result := e.db.
  3. WithContext(ctx).
  4. Preload("TokenDetails", "token_details.enabled = true").
  5. Preload("TokenDetails.Chains", "chains.enabled = true").
  6. 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:

确定