0

I have a kind of weird question to all of you who are Google Datastore expert. I've been studying the code of a working Google App Engine app. I found these lines of codes which make me confused.

guestbook_key = ndb.Key(Greeting, DEFAULT_GUESTBOOK_NAME)
mykey = ndb.Key(   Greeting, # kind 
                str(i+1), # id
                parent=guestbook_key # parent
                )
g = Greeting(key=mykey)`

My question is: Is it possible using, as a parent, the key of an entity which doesn't exist? I say it because no entity with key "guestbook_key" has been created (i searched in the entire code but i didn't find anything) Is it created only for giving a common root to entities without creating an entity root?

Michael Meyer
  • 2,179
  • 3
  • 24
  • 33

2 Answers2

0

Yes. From Using the ancestor path in the key (emphasis mine):

ndb.Key('Account', 'sandy@example.com', 'Message', 123, 'Revision', '1')

...

In the sample, ('Account', 'sandy@foo.com'), ('Message', 123), and ('Revision', '1') are all examples of kind-identifier pairs.

Notice that Message is not a model class; it is used only as a way to group revisions, not to store data.

...

You can use the named parameter parent to designate any entity in the ancestor path directly. All of the following notations represent the same key:

ndb.Key('Account', 'sandy@example.com', 'Message', 123, 'Revision', '1')

ndb.Key('Revision', '1', parent=ndb.Key(
    'Account', 'sandy@example.com', 'Message', 123))

ndb.Key('Revision', '1', parent=ndb.Key(
    'Message', 123, parent=ndb.Key('Account', 'sandy@example.com')))

In the quoted example above the Message entity keys are used as ancestor/parent keys for Revision entities without the Message entities actually existing.

Another such example, this time showing the root (common ancestor) of an entire entity group not actually existing: What would be the purpose of putting all datastore entities in a single group?

Community
  • 1
  • 1
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
0

Key is just an object in your program's memory even before anything is written to the datastore.

When you write an entity to the datastore, you will need to supply the key (complete, or incompletely if you want auto ID), and the data itself.

user7180
  • 3,756
  • 2
  • 22
  • 26