2015-06-06 60 views
0

我有一個傳統的谷歌應用程序引擎代碼,這是其在Python是正確的方式下獲得唯一的ID在App Engine數據存儲

以下實體類
class AffiliateParent(db.Model): 
    name = db.StringProperty(required = True) 

class Affiliate(db.Model): 
    email = db.StringProperty(required = True) 
    point_gain = db.IntegerProperty() 
    point_used = db.IntegerProperty() 
    #feature_upgrade = db.ListProperty(str) 
    modified_time = db.DateTimeProperty(auto_now = True) 

我想知道,是下面的正確的方式來生成唯一的ID?它們在Affiliate表中保證是唯一的嗎?

affiliate_parent_key = AffiliateParent.all(keys_only=True).filter('name =', 'yancheng').get(); 
affiliate_parent = db.get(affiliate_parent_key); 
# check whether affiliate exist, if not create one 
affiliate_parent = db.get(affiliate_parent_key); 

q = Affiliate.all() 
q.ancestor(affiliate_parent) 
q.filter('email =', email) 
affiliate = q.get() 

if not affiliate: 
    affiliate = Affiliate(
     email = email, 
     point_gain = 0, 
     point_used = 0, 
     parent = affiliate_parent 
    ) 
    affiliate.put() 

# 4503602445942784 
# is this unique? 
unique_id = affiliate.key().id() 

回答

1

是的,如果你不提供idkey實例化模型時,則數據存儲將生成一個唯一的ID,並將其分配給您實體.put()它...因此affiliate.key.id()將是獨一無二的

你也可以使用allocate_ids(count)

https://cloud.google.com/appengine/docs/python/datastore/functions#allocate_ids

+1

看起來像它的實體組中生成唯一的ID,所以它只能在實體d組的獨特由'affiliate_parent'定義,對吧? – tx802

+0

是...只在實體組內唯一 –

相關問題