You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Golang+GORM操作SQLite执行迁移时遇错误求助

问题解决方法

出现undefined: sqlite3.ErrNoExtended和undefined: sqlite3.Error错误的核心原因是gorm.io/driver/sqlite与github.com/mattn/go-sqlite3版本不兼容,按以下步骤修复:

  1. 清理Go模块缓存
    执行命令:
go clean -modcache
  1. 更新到兼容的依赖版本
    执行以下命令拉取最新的兼容版本:
go get gorm.io/driver/sqlite@latest
go get github.com/mattn/go-sqlite3@latest

或者手动在go.mod中指定兼容版本,示例:

require (
    github.com/mattn/go-sqlite3 v1.14.20
    gorm.io/driver/sqlite v1.5.3
    gorm.io/gorm v1.25.5
)

指定版本后执行go mod tidy同步依赖。

  1. 验证修复
    重新运行代码,错误即可消除。

额外优化建议

你的UserFlags结构体关联可添加约束保证数据一致性:

type UserFlags struct {
    gorm.Model
    UserID           int
    LastModuleChosen string `gorm:"column:last_module_chosen"`
    User             User   `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}

内容的提问来源于stack exchange,提问作者NightBottle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 01:28:33