如何排除某一列不被选中。

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

How to exclude a column from being Selected

问题

我正在使用Go作为编程语言,使用gorm作为ORM。

这是结构体模型:

type Product struct {
   ID        int     `gorm:"primaryKey" json:"id"`
   Name      string  `gorm:"notNull;size:125" json:"name"`
   Inventory int     `gorm:"-:migration" json:"inventory"`
}

当我执行如下的Join操作时:

DB.Joins("Product")

在select语句中,会出现inventory字段,导致类似以下错误:

Unknown column 'Product.inventory' in 'field list'

当然,由于标签"-:migration"的存在,它在products表中并不存在。

我想知道应该使用什么标签来使Inventory在select语句中被忽略/排除?

我只想在select语句中排除inventory字段,但在解析为JSON时,仍希望该字段存在。

英文:

I am using Go as language code and gorm as ORM

This is the struct model:

type Product struct {
   ID        int     `gorm:"primaryKey" json:"id"`
   Name      string  `gorm:"notNull;size:125" json:"name"`
   Inventory int     `gorm:"-:migration" json:"inventory"`
}

when I perform Join like this:

DB.Joins("Product")

the inventory field is present in select statement cause an error something like:

> Unknown column 'Product.inventory' in 'field list'

of course it doesn't exist in products table because of this tag "-:migration"

what tag should I use to make Inventory being ignored/exclude in select statement???

I only want to exclude inventory from select statement, I want the field still there when parsed to json

答案1

得分: 1

你应该使用的标签是gorm:"-:all",除了迁移权限之外,还包括读写权限。你可以在官方文档中了解更多信息:https://gorm.io/docs/models.html#Fields-Tags。我将与你分享我编写的用于测试的代码:

package main

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type Product struct {
	ID        int    `gorm:"primaryKey" json:"id"`
	Name      string `gorm:"notNull;size:125" json:"name"`
	Inventory int    `gorm:"-:all" json:"inventory"`
}

func main() {
	dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	db.AutoMigrate(&Product{})

	db.Create(&Product{Name: "pen"})
	db.Create(&Product{Name: "box"})

	var product Product
	db.Find(&product, 1)

    // 在这里你仍然可以访问和设置JSON。
    // 显然,该值无法从数据库中检索,因为它在数据库中不存在
	product.Inventory = 22 

	data, _ := json.Marshal(product)

	fmt.Println(string(data))
}

如果有帮助,请告诉我,谢谢!

英文:

The tag that you should use is gorm:"-:all" that also involves read/write permission other than the migration one.
You can read more about this in the official doc: https://gorm.io/docs/models.html#Fields-Tags
I'll share with you the code I wrote to test this out:

package main

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type Product struct {
	ID        int    `gorm:"primaryKey" json:"id"`
	Name      string `gorm:"notNull;size:125" json:"name"`
	Inventory int    `gorm:"-:all" json:"inventory"`
}

func main() {
	dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	db.AutoMigrate(&Product{})

	db.Create(&Product{Name: "pen"})
	db.Create(&Product{Name: "box"})

	var product Product
	db.Find(&product, 1)

    // here you can still access and set the JSON. 
    // Obviously, the value cannot be retrieved from the DB as it doesn't exist there
	product.Inventory = 22 

	data, _ := json.Marshal(product)

	fmt.Println(string(data))
}

Let me know if this helps, thanks!

huangapple
  • 本文由 发表于 2022年12月29日 18:50:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/74950158.html
匿名

发表评论

匿名网友

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

确定