google-app-engine


Getting Invalid Key message thrown when creating child records


I am having problems saving programs records when using a parent key for the account.
This code fails with error "invalid key" (see bottom for complete):
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
This passes:
key := datastore.NewIncompleteKey(ctx, "programs", nil)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
Complete code:
// insert test account
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
_, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
if err != nil {
log.Errorf(ctx, "Insert test account %v", err.Error())
c.AbortWithError(http.StatusInternalServerError, err)
return
}
var names = []string {"Low Impact", "Running"}
for i := 0; i < len(names); i++ {
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
if err != nil {
log.Errorf(ctx, "Insert test programs %v", err.Error())
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
The problem is that when you create an incomplete key:
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
Which you use to save an entity:
_, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
It works, but note that if the key passed is an incomplete key (it is in your case), datastore.Put() returns a new, unique key generated by the datastore. You don't store the returned new key, but you should!
When you try to create and save "programs" entities:
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
datastore.NewIncompleteKey() expects either a nil parent key, of if it is provided, it must be a complete key (cannot be incomplete). You pass actKey which is an incomplete key, hence the "invalid key" error message.
Solution is simple: store the generated new key, and pass the new, complete key as the parent key:
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
actKey, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
If err is nil, actKey will now be a complete key and therefore can be used as parent key when creating other keys with datastore.NewIncompleteKey() or datastore.NewKey().

Related Links

1.8.5 Prelease Accidentally in Production
Trying to get an SSL certificate from Comodo to work for Google App Engine
Login loop when adding a custom domain for appengine
InvalidClassException: No valid constructor
CookieJar does not catch incoming cookies
Google Cloud Console Projects, Advantage To Have Different Elements Under Same Project?
No api proxy found for service “datastore_v4” in new GAE 1.8.4
Which Google App Engine installer do I want to use for Windows?
Using Access Token from google cloud to login to Gmail
GAE : Yahoo, Google & Facebook login support
Google App Engine : JDO deletePersistent not consistent
Objectify 4 throwing strange error in GAE
How can I use my Google API email #developer.gserviceaccount.com
How to log messages in GAE go runtime?
Error with PHP SDK (Windows)
Increase Per-User Limit for Google API: Saving not possible, always error message “Your input was invalid”

Categories

HOME
sonarqube
forms
spring-data-redis
square
spring-session
android-json
missing-data
azure-container-service
retrofit2
frontend
nstableheaderview
flume
recordrtc
fhir
flatpak
google-data-studio
exploit
displaytag
interpreter
game-theory
tiki-wiki
winexe
calculation
cart
jmockit
code-snippets
card.io
hreflang
biztalk-rule-engine
indy10
video-capture
openerp-7
salesforce-communities
sim-card
password-protection
apriori
wmp
install.packages
file-sharing
rserve
payara-micro
quicktime
jtree
structuremap4
rocks
usart
wso2developerstudio
invoke-sqlcmd
spring-retry
dllimport
roadkill-wiki
matrix-inverse
alternate
mutual-authentication
sbcl
android-audiorecord
mt4j
mongohq
cffile
asplinkbutton
web-notifications
zope.interface
cuda-gdb
jsr286
mysql-error-1205
exponentiation
msr
hbm2java
blender-2.49
silverlight-oob
interop-domino
sifr3
entitykey
wspbuilder

Resources

Encrypt Message