Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
使用 GORM 无法向 PostgreSQL 数据库插入数据的原因是什么?如何解决?_创想鸟

使用 GORM 无法向 PostgreSQL 数据库插入数据的原因是什么?如何解决?

使用 gorm 无法向 postgresql 数据库插入数据的原因是什么?如何解决?

本文探讨在使用 GORM 向 PostgreSQL 数据库插入数据时遇到的问题,特别是错误信息 “failed to encode args[3]: unable to encode 1 into text format for varchar (oid 1043): cannot find encode plan” 的解决方法。

问题描述:

在使用 GORM 操作 PostgreSQL 数据库时,向 menu 表插入数据失败,报错信息提示无法将整数 1 编码为 varchar 类型。

数据库结构 (DDL):

CREATE TABLE "public"."menu" (  "id" INT4 NOT NULL GENERATED ALWAYS AS IDENTITY (    INCREMENT 1    MINVALUE 1    MAXVALUE 2147483647    START 1  ),  "title" VARCHAR(255) COLLATE "pg_catalog"."default",  "router" VARCHAR(255) COLLATE "pg_catalog"."default",  "state" VARCHAR(255) COLLATE "pg_catalog"."default",  "sort" INT4,  "icon" VARCHAR(255) COLLATE "pg_catalog"."default",  "created_at" TIMESTAMP(6),  "updated_at" TIMESTAMP(6),  "sid" INT4 NOT NULL DEFAULT 0);ALTER TABLE "public"."menu" OWNER TO "postgres";

数据库连接:

dsn := "host=xxxx user=postgres password=xxxxx dbname=xxxx port=5432 sslmode=disable timezone=asia/shanghai"db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})if err == nil {    db = db}

GORM 结构体定义 (初始):

type menumodel struct {    id         int       `gorm:"column:id" json:"id"`    title      string    `gorm:"column:title" json:"title"`    router     string    `gorm:"column:router" json:"router"`    sid        int       `gorm:"column:sid" json:"sid"`    state      int       `gorm:"column:state" json:"state"`    sort       int       `gorm:"column:sort" json:"sort"`    icon       string    `gorm:"column:icon" json:"icon"`    created_at time.time `gorm:"column:created_at;autocreatetime" json:"created_at"`    updated_at time.time `gorm:"column:updated_at;autoupdatetime" json:"updated_at"`}

插入操作:

initdb()menu := menumodel{    title:  "1",    router: "1",    sid:    1,    state:  1,    sort:   1,    icon:   "1",}db.Create(&menu)fmt.Println(menu.id)

问题分析与解决方案:

错误信息指出,state 字段的类型不匹配。数据库中 state 为 VARCHAR 类型,而 GORM 结构体中定义为 int 类型。 这导致 GORM 尝试将整数直接转换为 VARCHAR,引发错误。

解决方法:

将 menumodel 结构体中的 state 字段类型修改为 string:

type MenuModel struct {    Id         int       `json:"id"`    Title      string    `json:"title"`    Router     string    `json:"router"`    Sid        int       `json:"sid"`    State      string    `json:"state"`    Sort       int       `json:"sort"`    Icon       string    `json:"icon"`    Created_at time.Time `gorm:"autoCreateTime" json:"created_at"`    Updated_at time.Time `gorm:"autoUpdateTime" json:"updated_at"`}

同时,gorm:"column:..." 标签在大多数情况下是冗余的,因为 GORM 会自动映射列名。 简化后的代码如上所示。 修改后,数据插入应该能够成功执行。

以上就是使用 GORM 无法向 PostgreSQL 数据库插入数据的原因是什么?如何解决?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1386234.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Go语言切片[:5:5]究竟是什么意思?
上一篇 2025年12月15日 06:07:43
Go语言包内文件和函数过多:如何组织才能兼顾性能和可维护性?
下一篇 2025年12月15日 06:08:03

相关推荐

发表回复

登录后才能评论
关注微信