douzhuo6931 2017-07-11 23:07
浏览 27
已采纳

在螺栓中找到一个

I have a Golang project and I am using Bolt as my Database. I found and started to use This project to handle the connection and operations over the database. My current problem is that I cannot make a search by one field and get one result.

My struct definition in Golang is:

type Component struct {
    ID          int      `storm:"increment"`
    Name        string   `storm:"id,unique"` // primary key
    Cars        []string `storm:"index"`  // this field will be indexed
    Houses      []string `storm:"index"`  // this field will be indexed
    Pets        []string `storm:"index"`  // this field will be indexed
    Children    []string `storm:"index"`  // this field will be indexed
    Level       int
}

At first, I know is strange to have one field ID and a field Name with the tag id, the thing is that I want the name as the id and possibly as the key. I made some inserts and everything is ok, I even retrieved all the values and it worked perfectly. Now, I want to get one element by his name, I have this:

name := "someComponent"
var component models.Component
err := db.One("Name", name, &component)

But it returns not found but I know that a component is stored with that name. In other hand, if I make the search with Find I get the element, but that command is to return many results, and I want only one, I have this:

var components []models.Component
err := db.Find("Name", name, &components)
  • 写回答

1条回答 默认 最新

  • dsgm5631 2017-07-13 21:08
    关注

    Drafting answer from my comment.

    Per library codebase, ID is getting in your way even though Name is defined as primary key.

    If you would like to keep Name as primary key, can you please remove the field ID from struct Component and then try method db.One.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏