登录
首页 >  Golang >  Go问答

解决 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学习网公众号!

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