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)
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
.