解决 Go 中出现的 SQL 扫描错误的方法
来源:stackoverflow
时间:2024-02-06 10:42:27 366浏览 收藏
哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《解决 Go 中出现的 SQL 扫描错误的方法》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!
使用的技术
- 走
- 戈姆
- postgresql 14.5(在 docker 容器中)
- 开放api
- oapi-codegen 软件包 v1.11.0
我正在为 shin megami tensei persona 衍生系列游戏中的 persona 构建一个用于 crud 操作的 api。我在尝试使用上述技术从数据库中获取数据时遇到问题。
错误消息
sql: scan error on column index 0, name "arcana_id": unsupported scan, storing driver.value type string into type *api.arcanaid
我认为问题在于,在检索数据时,它试图将字符串存储在 *api.arcanaid 中。
如何调整我的数据模型,以便可以从数据库中提取 uuid?
我已经看过这个问题,它没有解决我的问题,因为它正在处理零值。
我尝试将 arcanaid 的类型从字符串更改为 uuid.uuid,但没有成功。同样的错误消息。
数据模型 - openapi.yaml
components: schemas: p5arcana: type: object required: - arcanaid properties: arcanaid: $ref: "#/components/schemas/arcanaid" arcanaid: description: a universally unique identifier for identifying one of the 22 major arcana. type: string x-go-type: uuid.uuid x-go-type-import: path: github.com/google/uuid x-oapi-codegen-extra-tags: gorm: "primarykey;unique;type:uuid;default:uuid_generate_v4()"
界面interface.go
packages databases import ( "context" "github.com/bradleygamimarques/personagrimoire/api ) type personagrimoire interface { getpersona5arcanabyuuid(ctx context.context, arcanauuid api.arcanaid) (arcana api.p5arcana, err error) }
interfaceimplinterfaceimpl.go
packages databases import ( "context" "errors" "fmt" "github.com/bradleygamimarques/personagrimoire/api" "github.com/sirupsen/logrus" "gorm.io/gorm" ) type personagrimoireimpl struct { gorm *gorm.db logger *logrus.logger } func (p *personagrimoireimpl) getpersona5arcanabyuuid(ctx context.context, arcanauuid api.arcanaid) (arcana api.p5arcana, err error) { err = p.gorm.withcontext(ctx).model(&api.p5arcana{arcanaid: arcana.arcanaid}).where(&api.p5arcana{arcanaid: arcanauuid}).first(&arcana).error if err != nil { if errors.is(err, gorm.errrecordnotfound) { p.logger.warnf("attempted to get persona 5 arcana by id that does not exist. error: %s", err.error()) return api.p5arcana{}, fmt.errorf("attempted to get persona 5 arcana by id that does not exist error: %w", err) } } return arcana, nil }
实现代码
// Check if ID exists // Calls GetPersona5ArcanaByUUID() // Return result
正确答案
感谢 jamie tanna,电话 https://www.jvt.me/posts/2022/07/12/go-openapi-server/。
他们的解决方案涉及不使用 github.com/google/uuid 包,而是使用 openapi_types.uuid 类型。
这是通过定义架构来完成的。
arcanaid: description: a universally unique identifier for identifying one of the 22 major arcana. type: string format: uuid pattern: "[a-fa-f0-9]{8}-[a-fa-f0-9]{4}-4[a-fa-f0-9]{3}-[89abab][a-fa-f0-9]{3}-[a-fa-f0-9]{12}" x-oapi-codegen-extra-tags: gorm: "type:uuid;primarykey"
这会生成如下所示的代码
// ArcanaID A universally unique identifier for identifying one of the 22 Major Arcana. type ArcanaID = openapi_types.UUID
以上就是《解决 Go 中出现的 SQL 扫描错误的方法》的详细内容,更多关于的资料请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习