登录
首页 >  Golang >  Go问答

检测 uuid 列中的错误

来源:stackoverflow

时间:2024-03-03 11:00:26 432浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《检测 uuid 列中的错误》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我有以下 postgresql 数据库模型:

smallstruct struct {
    firstuuid  uuid.uuid   `gorm:"column:first_id"`
    listofuuid []uuid.uuid `gorm:"column:list_of_id;type:uuid[]"`
}

bigstruct struct {
    smallstruct
    id         uuid.uuid  `gorm:"primarykey;column:id;type:uuid;default:gen_random_uuid()"`
    myownfield string `gorm:"column:my_own_field"`
}


func (bigstruct) tablename() string {
    return "my_schema.my_table"
}

这是数据库中的表定义:

column             |            type             | collation | nullable |            default            
--------------------------------+-----------------------------+-----------+----------+-------------------------------
 id                             | uuid                        |           | not null | my_schema.uuid_generate_v4()
 first_id                       | uuid                        |           | not null | 
 my_own_field                   | string                      |           | not null | 
 list_of_id                     | uuid[]                      |           |          |

我有以下代码从表中提取一条数据:

func getdata(id uuid.uuid) (*models.smallstruct, error) {
    var data models.smallstruct

    err := db.model(&models.bigstruct{}).where("first_id = ?", id).find(&data).error

    return &data, parseerror(err)
}

当我尝试使用上面的代码从此表中获取数据时,我收到字段 list_of_id 的错误:

sql: Scan error on column index 11, name "list_of_id": unsupported Scan, storing driver.Value type string into type *[]uuid.UUID

什么会导致这种情况?


正确答案


我已经找到解决办法了。我应该使用 "github.com/lib/pq" 库中的 pq.stringarray

SmallStruct struct {
    FirstUUID  uuid.UUID      `gorm:"column:first_id"`
    ListOfUUID pq.StringArray `gorm:"column:list_of_id;type:uuid[]"`
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《检测 uuid 列中的错误》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>