英文:
How do I store keys in a datastore object using Go?
问题
我正在尝试将一个名为"Location"的键放入一个名为"Band"的结构体中。当我首次创建一个Location并添加键后,尝试检索Location的值时,所有字符串都为空。如果我添加一个新的Band,我创建的Location就会正常显示,并且在新的Band中使用该Location也能正常工作。我正在使用不完整的键。
在添加Location时的代码如下:
func AddLocation(value Location, rq *http.Request) (*datastore.Key, error) {
c := appengine.NewContext(rq)
key := datastore.NewIncompleteKey(c, config.LOCATION_TYPE, nil)
_, err := datastore.Put(c, key, &value)
return key, err
}
使用现有的Location的代码如下:
case "existing":
rawId := rq.FormValue("location_id")
q := strings.Split(rawId, ",")
x := q[1]
id_int, e := strconv.ParseInt(x, 10, 64)
if e != nil {
message = e.Error()
}
locationId = datastore.NewKey(c, config.LOCATION_TYPE, "", id_int, nil)
// message = "not implemented yet"
break
使用原始Put操作返回的键似乎不起作用,所以我尝试了以下方法:
case "new":
location := model.Location{rq.FormValue("city"), rq.FormValue("state"), rq.FormValue("country")}
var err error
_, err = model.AddLocation(location, rq)
if err != nil {
message = "Location add: " + err.Error()
}
q := datastore.NewQuery(config.LOCATION_TYPE).Filter("City =", location.City).
Filter("State =", location.State).Filter("Country =", location.Country).
KeysOnly()
keys, err := q.GetAll(c, nil)
if err != nil {
message = "Location add: " + err.Error()
}
var k *datastore.Key
for _, key := range keys {
k = key
break
}
locationId = k
break
但这个方法也没有正常工作。我错过了什么?
英文:
I'm trying to put a *Key of a Location
type Location struct
City, State, Country string
}
in a Band
type Band struct {
Name string
LocationId *datastore.Key
Albums []Album
}
When I first create a Location, add the key, then try to retrieve the Location values, the strings all come out empty. If I then add a new Band, the location I created shows up fine, and using that location in a new Band works. I'm using incomplete keys:
func AddLocation(value Location, rq *http.Request) (*datastore.Key, error) {
c := appengine.NewContext(rq)
key := datastore.NewIncompleteKey(c, config.LOCATION_TYPE, nil)
_, err := datastore.Put(c, key, &value)
return key, err
}
Using an existing Location is as follows:
case "existing":
rawId := rq.FormValue("location_id")
q := strings.Split(rawId, ",")
x := q[1]
id_int, e := strconv.ParseInt(x, 10, 64)
if e != nil {
message = e.Error()
}
locationId = datastore.NewKey(c, config.LOCATION_TYPE, "", id_int, nil)
// message = "not implemented yet"
break
Using the key from the original Put didn't seem to work, so I resorted to:
case "new":
location := model.Location{rq.FormValue("city"), rq.FormValue("state"), rq.FormValue("country")}
var err error
_, err = model.AddLocation(location, rq)
if err != nil {
message = "Location add: " + err.Error()
}
q := datastore.NewQuery(config.LOCATION_TYPE).Filter("City =", location.City).
Filter("State =", location.State).Filter("Country =", location.Country).
KeysOnly()
keys, err := q.GetAll(c, nil)
if err != nil {
message = "Location add: " + err.Error()
}
var k *datastore.Key
for _, key := range keys {
k = key
break
}
locationId = k
break
And that didn't work right either. What am I not getting?
答案1
得分: 1
当您使用不完整的键调用datastore.Put
时,键的id字段不会被填充。返回的键会包含id字段。您正在忽略datastore.Put
的返回值。
您的AddLocation函数应该像这样:
func AddLocation(value Location, rq *http.Request) (*datastore.Key, error) {
c := appengine.NewContext(rq)
key := datastore.NewIncompleteKey(c, config.LOCATION_TYPE, nil)
// 这一行代码会使用准确的信息更新键
key, err := datastore.Put(c, key, &value)
return key, err
}
英文:
When you call datastore.Put
with an incomplete key, the key's id is not filled in. The returned key has it. You're ignoring the return value from datastore.Put
with _.
Your AddLocation func should look like this:
func AddLocation(value Location, rq *http.Request) (*datastore.Key, error) {
c := appengine.NewContext(rq)
key := datastore.NewIncompleteKey(c, config.LOCATION_TYPE, nil)
// this line updates the key with accurate information
key, err := datastore.Put(c, key, &value)
return key, err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论