在 Google App Engine 数据存储区(对于 Python),ndb 键 ID 默认总是整数吗?
In Google App Engine datastore (for Python), are ndb key ids by default always integers?
似乎是这样,但我无法在文档中确认。
ndb Key class 包括方法 id
、string_id
和 integer_id
。在 id
中,它表示:
Returns the string or integer id in the last (kind, id) pair, or None if the key is incomplete.
我假设 string_id
只有 returns 当 Key 构造函数显式传递一个字符串 id 时是一个有效的 id。
我确认是这样的。它在文档 here:
中说明(有些人可能会说,埋没)
An application can create an entity without specifying an ID; the Datastore automatically generates a numeric ID.
代码示例:
from google.appengine.ext import ndb
class Fingerprint(ndb.Model):
fingerprint = ndb.StringProperty(required=True)
fingerprint = Fingerprint(fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())
fingerprint = Fingerprint(id='abc', fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())
fingerprint = Fingerprint(id=100, fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())
输出:
4785074604081152 <type 'long'>
abc <type 'str'>
100 <type 'long'>
似乎是这样,但我无法在文档中确认。
ndb Key class 包括方法 id
、string_id
和 integer_id
。在 id
中,它表示:
Returns the string or integer id in the last (kind, id) pair, or None if the key is incomplete.
我假设 string_id
只有 returns 当 Key 构造函数显式传递一个字符串 id 时是一个有效的 id。
我确认是这样的。它在文档 here:
中说明(有些人可能会说,埋没)An application can create an entity without specifying an ID; the Datastore automatically generates a numeric ID.
代码示例:
from google.appengine.ext import ndb
class Fingerprint(ndb.Model):
fingerprint = ndb.StringProperty(required=True)
fingerprint = Fingerprint(fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())
fingerprint = Fingerprint(id='abc', fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())
fingerprint = Fingerprint(id=100, fingerprint='abc')
new_key = fingerprint.put()
print new_key.id(), type(new_key.id())
输出:
4785074604081152 <type 'long'>
abc <type 'str'>
100 <type 'long'>