


Go error: time.Time does not implement driver.Valuer (method Value is missing)
php editor Zimo may encounter a common error during development using the Go language: "Go error: time.Time does not implement driver.Valuer (method Value is missing) )". This error usually occurs when storing values of type time.Time into the database. It shows that the time.Time type does not implement the driver.Valuer interface and is missing the Value method. There are many ways to solve this problem. We can customize a new time type or use a third-party library to handle the storage of time types. In this article, we will introduce the cause and solution of this error in detail to help developers better understand and solve this problem.
Question content
I'm working on a Go project where I'm using sqlboiler to generate code from a SQLite3 database I created using a setup.sh script. I've encountered an error that I can't seem to resolve. The error message is:
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)
This error occurs when I try to use the time.Time value in the graph/db/repositories.go file. I'm using golang:1.21-alpine3.18 Docker image for my project.
I try to change the created_at column type in the repository table of the SQLite3 database. I've tested it using TEXT and TIMESTAMP data types but the error persists.
Does anyone know what might be causing this error and how to fix it?
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"
environment
・ MacBook Pro M1 Pro
Workaround
I would say update the repository structure in Go to have CreatedAt as the time.Time field. When assigning a value to CreatedAt, use the time.Time value. For example, currTime := time.Now() Make your structure like this
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... }
Then set the CreatedAt value:
repository := &Repository{ ID: "some-id", Owner: "owner-id", Name: "repo-name", CreatedAt: time.Now(), // setting it as time.Time }
The above is the detailed content of Go error: time.Time does not implement driver.Valuer (method Value is missing). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Why does map iteration in Go cause all values to become the last element? In Go language, when faced with some interview questions, you often encounter maps...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...
