简体   繁体   English

使用 gorm 查询字符串数组

[英]Querying string array with gorm

I'm trying to select items of a table with a column of type text[] .我正在尝试 select 列类型为text[]的表的项目。 The result of the field brandIds always return null字段brandIds的结果总是返回null

my struct:我的结构:

type User struct {
    Email    string         `json:"email"`
    Name     string         `json:"name"`
    BrandIds pq.StringArray `gorm:"type:text[]" json:"brandIds"`
}

the code:编码:

        var users []dtos.User
        db.Table("user").Find(&users)
        data, _ := json.Marshal(users)

Name and emails returns fine... But the brandIds always return null.姓名和电子邮件返回正常......但brandIds总是返回null。 Type of brandIds column is text[] brandIds 列的类型是text[]

Postgres have some troubles with camelCase column names. Postgres 在使用 camelCase 列名方面存在一些问题。 When I named BrandIds gorm was looking for column bran_id当我命名BrandIds时,gorm 正在寻找列bran_id

I needed to specify the column name so I have added the gorm:"column:brandId" to tell gorm whats the name of the column.我需要指定列名,所以我添加了gorm:"column:brandId"来告诉 gorm 列的名称是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM