无法找到使用Go/SQLX存在的结构属性。

huangapple go评论162阅读模式
英文:

Cannot find Struct property that exists using Go/SQLX

问题

我正在从文件上传中收集元数据,并将其插入到数据库表中。结构如下:

// 文件元数据结构
type MetaData struct {
Owner string
FileRows int64
FileSize string
FileName string
FileUuid string
LastUpdated string
}

以下是我如何填充对结构的引用:

metaDataRow := MetaData{
Owner: "Fake Name",
FileRows: (int64)(count - 1),
FileSize: fileSize,
FileName: fileName,
FileUuid: handle,
LastUpdated: time.Now().Format(time.RFC822Z),
}

我的问题是,当我尝试将这个结构引用插入到数据库时,我收到一个错误消息:

在 &processor.MetaData{Owner:"Fake Name", FileRows:1499, FileSize:"308.9 kB", FileName:"small-file.csv", FileUuid:"1234567890qwerty", LastUpdated:"30 Jan 20 21:13 +0000"} 中找不到名称 Owner。

现在,正如我们可以清楚地看到的,Owner 在结构中是存在的,并且它有一个值,其他字段也是如此。我不确定查询赋值是否在第一次查找时失败并导致了 panic,但我无法继续执行以查看其他字段是否也失败。这是我的 sqlx NamedExec,可能存在结构引用或绑定的问题:

// 执行事务
_, err = tx.NamedExec(INSERT INTO file_metadata ( owner, file_rows, file_size, file_name, file_uuid, last_updated ) VALUES ( :Owner, :FileRows, :FileSize, :FileName, :FileUuid, :LastUpdated ), &metaDataRow)

我希望这是一个容易修复的问题,可能是拼写错误。

英文:

I am gathering metadata from a file upload, and inserting into a DB table. The struct is as such:

// file Metadata struct
type MetaData struct {
	Owner       string
	FileRows    int64
	FileSize    string
	FileName    string
	FileUuid    string
	LastUpdated string
}

And here is how I am populating a reference to the struct:

metaDataRow := MetaData{
	Owner:       "Fake Name",
	FileRows:    (int64)(count - 1),
	FileSize:    fileSize,
	FileName:    fileName,
	FileUuid:    handle,
	LastUpdated: time.Now().Format(time.RFC822Z),
}

My issue is that when I try to insert this struct ref into my db, I get an error message:

> could not find name Owner in &processor.MetaData{Owner:"Fake Name", FileRows:1499, FileSize:"308.9 kB", FileName:"small-file.csv", FileUuid:"1234567890qwerty", LastUpdated:"30 Jan 20 21:13 +0000"}

Now, as we can clearly see, Owner exists in the struct and it has a value, as do the others. I am not sure if the query assignment failed on the first lookup and panic'd there, but I can't get past this to see if the others fail as well. Here is my sqlx NamedExec as there may be an issue with my Struct reference, or the binding?

// execute transaction
_, err = tx.NamedExec(`
	INSERT INTO file_metadata (
		owner,
		file_rows,
		file_size,
		file_name,
		file_uuid,
		last_updated
	) VALUES (
		:Owner,
		:FileRows,
		:FileSize,
		:FileName,
		:FileUuid,
		:LastUpdated
	)
`, &metaDataRow)

I am hoping this is an easy fix, borderline typo.

答案1

得分: 2

你可以将 db tag 添加到你的结构体中:

 Owner string `db:"owner"`

然后在插入时使用:

...VALUES (:owner, ....
英文:

You could add the db tag to your struct:

 Owner string `db:"owner"`

and then on the insert:

...VALUES (:owner, ....

huangapple
  • 本文由 发表于 2020年2月5日 10:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/60068381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定