appengine datastore id and key_name - google-app-engine

In Google appengine datastore, Is it possible to define both id and key_name for one entry?
The documantation of Key.from_path says that only can define id or key_name for it, so which means that an entry can has id or key_name, right?

No. You can one or the other, but not both.
See http://code.google.com/appengine/docs/python/datastore/entities.html#Kinds_IDs_and_Names

The entity ID is part of the key. This can be an arbirary string specified by the app, or it can be generated automatically by the datastore. The API calls an entity ID given by the app a key name, and an entity ID generated by the datastore an ID. An entity has either a key name or an ID, but not both. The ID is populated when the entity object is saved to the datastore for the first time.

Related

Generating a unique id GAE datastore

In MySQL I used auto-increment to generate an id for every user. I would like to create a similar user table in Google Datastore where the id for a user will be unique. According to these docs:https://cloud.google.com/appengine/docs/java/datastore/entities
System-allocated ID values are guaranteed unique to the entity group.
But according to this post: Ever see duplicate IDs when using Google App Engine and ndb? the id's are not unique. I need this id to be unique. It is confusing because in the docs it says the id is unique, but from this post it says the id is not unique it is the key that is unique. My objective is for no two users to have the same id. How can I guarantee this? I would prefer for the database to take care of this form me opposed to me having to create large ids manually using things such as uuids.
As Igor correctly observed, IDs are always unique as long as the entity has no parent.
I can't think of any reason to make user entities children of some other entities, so you are safe.
Note that IDs will not be sequential, as it helps to spread the load equally across the entire dataset - it's a by-product of how the Datastore is designed.

Google App Engine about key in ndb

I am new in web development and also in google app engine. I dont really understand what the function of key in ndb, i think it is like a primary key for each row? If the function of key is to identified any rows in each entities, can I use it to do the query?
I also do not understand how to retrieve the key in each rows. In the docs we should use this code to return key, and also store to database.
sandy_key = sandy.put()
What if i do not want to store anything but i only want to retrieve the key. For example:
class Post(ndb.Model):
title = ndb.StringProperty()
content = ndb.TextProperty()
created = ndb.DateTimeProperty(auto_now=True)
And then I do a query.
q = Post.query(Post.title == "test")
Is it possible to get the key from that query?
Thank you
You are actually looking for the entity's id (or identifier). An entity's key is made up of an identifier and a kind. The kind is typically the name of the Model, which in your case is "Post".
You can retrieve an entity's id using the .id() method. In your example, you must first "fetch" or "get" an entity. You provided a query. If you were to fetch one entity matching your query and find the id of that entity, your code would look like this:
q = Post.query(Post.title == "test")
my_entity = q.fetch(1, keys_only=True)
my_key = my_entity.id()
The "keys_only" parameter in the fetch is not necessary, but it does save resources because it tells the fetch to only retrieve the key.
This information is explained in the NDB Entity Keys documentation.

Why does Google AppEngine have two primary keys, 'key' and 'id/name'?

If you leave one or the other empty, or don't specify in your Entity, it creates a key/id for that entity anyways, as seen in the admin console datastore viewer.
Bonus question: Why can't you get the ID for an Entity object after you put() it? entity.getProperty("id") returns null. Key objects cannot be serialized so cannot be used by GWT.
Reference:
https://developers.google.com/appengine/docs/java/datastore/entities
https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata#Keys
Entities have a Key, and Keys (of persisted entities) have either auto-assigned ids, or programmer-supplied names. The name/id is a property of the Key, not a property of the Entity.
Instead of entity.getProperty("id") in Java you write entity.getKey().getId() (or .getName() if you gave the key a name).
The lower-level details are in:
https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Entity
https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Key
`

Is there any way to have key_name and id at the same time for GAE datastore entity?

In addition to the key_name I generate, I also would like to have some other property, which will act as id (I don't want to show key_name to the user). Can it be id? Or, how to generate unique value instead of id?
What I will do - I will generate a url with usage of that id and parent key name. If user clicks on this link, I'll need to find this datastore entity and update it.
Here is the current code.
Creation of the record:
item = Items.get_by_key_name(key_names=user_id, parent=person)
if item is None:
item = Items(key_name='id'+user_id, parent=person)
Getting the record:
item = Items.get_by_key_name(key_names=user_id, parent=person)
user_id is what should be hidden.
I could be probably wrong because your requirements are not clear, but for me you should pass just the key to the view using:
item.key()
then you could pass back the key to the controller and easily retrieve a given entity with:
item = Items.get(key)
Entities have exactly one of a key name or ID - never both. You could create an entity with a single ReferenceProperty pointing to your target entity, and use its ID as an identifier, but there really should be no reason not to reveal a key name to a user - a well authored app should not rely on this value remaining secret.
Note that it's trivially easy to extract the key name (and the rest of the information about a key) from the string encoded key.

Key or Long for ID in Google App Engine (JDO)

I'm using JDO with Google App Engine for storage and I'm wondering what is the difference between the Key object and Long for id?
I find the long ID more practical, am I missing anything?
Thanks.
A Key is globally uniquely identifier which uniques identifies an entity across all of app engine. It consists of two pieces:
A path describing what app the entity belongs to, any ancestor keys, and the entity's kind.
An ID (a long) or a key name (a string).
Regardless of whether you choose to use a long or a string as the second piece, there is a Key object is associated with every entity stored in the datastore.

Resources