我正在进行一个Go项目,在该项目中,我使用SQLBOOBER从我用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)

当我try 使用graph/db/Repositories.go文件中的time.Time值时,出现此错误.我在我的项目中使用了Golang:1.21-alpine3.18 Docker图像.

我try 在我的SQLite3数据库的Repositories表中更改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 to Have CreatedAt as a Time字段中说更新您的存储库 struct . 为CreatedAt赋值时,请使用time.Time值.例如,curTime:=time.Now() 使您的 struct 如下所示

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相关问答推荐

如何在Deliverq中高效地传达客户依赖关系?

如何在使用中介资源时处理函数中的`defer`

Golang html/模板&需要错误数量的参数1在模板中使用';调用';获得0&q;

为什么工具链指令在这种情况下没有效果?

带有一个新变量的Go冒号等于运算符

无法在32位计算机上运行Golang应用程序

如果第一次匹配条件,如何跳过切片中的值

Golang:访问any类型泛型上的字段

如何获取集群外go Kubernetes客户端的当前命名空间?

Go 中的 protobuf FieldMask 解组

Go - 永远停止带有上下文的循环

如何使用 sync.WaitGroup 来执行所有的 goroutine?

你如何在 Golang 代码中测试 filepath.Abs​​ 失败?

具有嵌套重复的正则表达式

Go http.FileServer 给出意外的 404 错误

如何使用 fyne Go 使用 canvas.NewText() 使文本可滚动

使用 Golang SQL 驱动程序连接到snowflake

自定义指标未显示在 prometheus web ui 中,grafana 中也是如此

如何在Golang中的差异函数中杀死命令Exec

Go 语言的select语句