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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)