Hi cryb,

As you noted, we do not currently allow the ID for a key to be set, as we
ensure that the ID is unique for each existing entity. I recommend using a
key name instead of an ID, as Antoniov suggeted, if possible.

It is technically possible to modify the key of an entity as it is being
converted to a protocol buffer message before it is sent to the datastore.
You could do this using hooks in the API proxy as described in this article:
http://code.google.com/appengine/articles/hooks.html Also it is possible to
construct the key for the desired object if you know the ID in advance.

class X(db.Model):
  pass

# If you've already created the entity so you have the ID.
x_id = X().put().id()

# Instead of getting by ID, you can create the key manually.
k = db.Key.from_path('X', x_id)

Now you have the desired key without having fetched the object, but the part
which the model class does not allow is setting the key yourself. So you
could modify the protocol buffer message before it is written to the
datastore, but I don't recommend it.

The decision to allow setting key_names but not IDs is something we may
revisit.

Happy coding,

Jeff


2009/6/12 cryb <cbuti...@gmail.com>

>
> Hi.. that is to build key names... What I asked was why I can't build
> a key ID..
>
> On Jun 12, 5:35 am, Antoniov <nio....@gmail.com> wrote:
> > Use the code:
> > s = Story(key_name="xzy123")
> > Then you create an entity with the key name "xzy123".
> >
> > Check this:
> http://code.google.com/intl/en-US/appengine/docs/python/datastore/key...
> >
> > On 6月12日, 上午1时28分, cryb <cbuti...@gmail.com> wrote:
> >
> > > Does anyone know why it is possible to build a key name but NOT a key
> > > id? I know key IDs are used as autoincrements, but why can't I just
> > > override this mechanism and build my own key id?
> > > Suppose I want to overwrite an existent entry in my table that has a
> > > key id I know, and also I want to keep that key id after update...
> > > because I can't just build a key id, I am forced to fetch that entity,
> > > modify it and write it back, instead of just write the updated entity
> > > with the key id I already know - so an additional read to the
> > > datastore.
> > > Is there an obscure reason for that? (both key names and key ids are
> > > prefixed with appid/kind as far as I know so there is no chance of
> > > collision with other apps/kinds)
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to