php小编子墨在使用Go语言开发过程中,可能会遇到一个常见的错误:“Go错误:time.Time未实现driver.Valuer(缺少方法Value)”。这个错误通常在将time.Time类型的值存储到数据库中时出现。它表明time.Time类型没有实现driver.Valuer接口,并且缺少Value方法。解决这个问题的方法有很多,我们可以通过自定义一个新的时间类型,或者使用第三方库来处理时间类型的存储。在本文中,我们将详细介绍这个错误的原因和解决方案,帮助开发者更好地理解和解决这个问题。
问题内容
我正在开发一个 Go 项目,在该项目中,我使用 sqlboiler 从我使用 setup.sh 脚本创建的 SQLite3 数据库生成代码。我遇到了一个似乎无法解决的错误。错误信息是:
graph/db/repositories.go:998:23: cannot use o.CreatedAt (variable of type string) as driver.Valuer value in argument to queries.MustTime: string does not implement driver.Valuer (missing method Value) graph/db/repositories.go:999:23: cannot use &o.CreatedAt (value of type *string) as sql.Scanner value in argument to queries.SetScanner: *string does not implement sql.Scanner (missing method Scan)
当我尝试在 graph/db/repositories.go 文件中使用 time.Time 值时,会发生此错误。我正在为我的项目使用 golang:1.21-alpine3.18 Docker 映像。
我尝试更改 SQLite3 数据库的存储库表中的created_at 列类型。我已经使用 TEXT 和 TIMESTAMP 数据类型对其进行了测试,但错误仍然存在。
有谁知道可能导致此错误的原因以及如何解决它?
repositories.go
func (o *Repository) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { if o == nil { return errors.New("db: no repositories provided for insertion") } var err error if !boil.TimestampsAreSkipped(ctx) { currTime := time.Now().In(boil.GetLocation()) if queries.MustTime(o.CreatedAt).IsZero() { queries.SetScanner(&o.CreatedAt, currTime) } } if err := o.doBeforeInsertHooks(ctx, exec); err != nil { return err } nzDefaults := queries.NonZeroDefaultSet(repositoryColumnsWithDefault, o) key := makeCacheKey(columns, nzDefaults) repositoryInsertCacheMut.RLock() cache, cached := repositoryInsertCache[key] repositoryInsertCacheMut.RUnlock()
setup.sh
#!/usr/local/bin/sh set -eu readonly DBFILE_NAME="mygraphql.db" # Create DB file if [ ! -e ${DBFILE_NAME} ];then echo ".open ${DBFILE_NAME}" | sqlite3 fi # Create DB Tables echo "creating tables..." sqlite3 ${DBFILE_NAME} " PRAGMA foreign_keys = ON; CREATE TABLE IF NOT EXISTS users(\ id TEXT PRIMARY KEY NOT NULL,\ name TEXT NOT NULL,\ project_v2 TEXT\ ); CREATE TABLE IF NOT EXISTS repositories(\ id TEXT PRIMARY KEY NOT NULL,\ owner TEXT NOT NULL,\ name TEXT NOT NULL,\ created_at TEXT NOT NULL DEFAULT (DATETIME('now','localtime')),\ FOREIGN KEY (owner) REFERENCES users(id)\ ); CREATE TABLE IF NOT EXISTS issues(\ id TEXT PRIMARY KEY NOT NULL,\ url TEXT NOT NULL,\ title TEXT NOT NULL,\ closed INTEGER NOT NULL DEFAULT 0,\ number INTEGER NOT NULL,\ repository TEXT NOT NULL,\ CHECK (closed IN (0, 1)),\ FOREIGN KEY (repository) REFERENCES repositories(id)\ ); CREATE TABLE IF NOT EXISTS projects(\ id TEXT PRIMARY KEY NOT NULL,\ title TEXT NOT NULL,\ url TEXT NOT NULL,\ owner TEXT NOT NULL,\ FOREIGN KEY (owner) REFERENCES users(id)\ ); CREATE TABLE IF NOT EXISTS pullrequests(\ id TEXT PRIMARY KEY NOT NULL,\ base_ref_name TEXT NOT NULL,\ closed INTEGER NOT NULL DEFAULT 0,\ head_ref_name TEXT NOT NULL,\ url TEXT NOT NULL,\ number INTEGER NOT NULL,\ repository TEXT NOT NULL,\ CHECK (closed IN (0, 1)),\ FOREIGN KEY (repository) REFERENCES repositories(id)\ ); CREATE TABLE IF NOT EXISTS projectcards(\ id TEXT PRIMARY KEY NOT NULL,\ project TEXT NOT NULL,\ issue TEXT,\ pullrequest TEXT,\ FOREIGN KEY (project) REFERENCES projects(id),\ FOREIGN KEY (issue) REFERENCES issues(id),\ FOREIGN KEY (pullrequest) REFERENCES pullrequests(id),\ CHECK (issue IS NOT NULL OR pullrequest IS NOT NULL)\ ); " # Insert initial data echo "inserting initial data..." sqlite3 ${DBFILE_NAME} " PRAGMA foreign_keys = ON; INSERT INTO users(id, name) VALUES\ ('U_1', 'hsaki') ; INSERT INTO repositories(id, owner, name) VALUES\ ('REPO_1', 'U_1', 'repo1') ; INSERT INTO issues(id, url, title, closed, number, repository) VALUES\ ('ISSUE_1', 'http://example.com/repo1/issue/1', 'First Issue', 1, 1, 'REPO_1'),\ ('ISSUE_2', 'http://example.com/repo1/issue/2', 'Second Issue', 0, 2, 'REPO_1'),\ ('ISSUE_3', 'http://example.com/repo1/issue/3', 'Third Issue', 0, 3, 'REPO_1')\ ; INSERT INTO projects(id, title, url, owner) VALUES\ ('PJ_1', 'My Project', 'http://example.com/project/1', 'U_1')\ ; INSERT INTO pullrequests(id, base_ref_name, closed, head_ref_name, url, number, repository) VALUES\ ('PR_1', 'main', 1, 'feature/kinou1', 'http://example.com/repo1/pr/1', 1, 'REPO_1'),\ ('PR_2', 'main', 0, 'feature/kinou2', 'http://example.com/repo1/pr/2', 2, 'REPO_1')\ ; "
sqlboiler.toml
pkgname="db" output="graph/db" wipe=true add-global-variants=false no-tests=true [sqlite3] dbname = "./mygraphql.db"
环境
・ MacBook Pro M1 Pro
解决方法
我会说更新 Go 中的存储库结构,将 CreatedAt 作为 time.Time 字段。 将值分配给 CreatedAt 时,请使用 time.Time 值。例如, currTime := time.Now() 让你的结构像这样
type Repository struct { ID string `boil:"id" json:"id" toml:"id" yaml:"id"` Owner string `boil:"owner" json:"owner" toml:"owner" yaml:"owner"` Name string `boil:"name" json:"name" toml:"name" yaml:"name"` CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"` // other fields... }
然后设置 CreatedAt 值:
repository := &Repository{ ID: "some-id", Owner: "owner-id", Name: "repo-name", CreatedAt: time.Now(), // setting it as time.Time }
以上是Go错误:time.Time未实现driver.Valuer(缺少方法Value)的详细内容。更多信息请关注PHP中文网其他相关文章!

C 更适合需要直接控制硬件资源和高性能优化的场景,而Golang更适合需要快速开发和高并发处理的场景。1.C 的优势在于其接近硬件的特性和高度的优化能力,适合游戏开发等高性能需求。2.Golang的优势在于其简洁的语法和天然的并发支持,适合高并发服务开发。

Golang在实际应用中表现出色,以简洁、高效和并发性着称。 1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)