为不同类型的其他实体重新使用实体的ID - 理智的想法? [英] re-using an entity's ID for other entities of different kinds - sane idea?

查看:86
本文介绍了为不同类型的其他实体重新使用实体的ID - 理智的想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的(python)应用程序正在使用多个实体,其中许多实体是1:1关系。例如:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ #peer_key = ndb.KeyProperty(kind ='Peer')

class Peer(ndb.model):
field2 = ndb.InteProProperty()
#main_key = ndb.KeyProperty (kind ='Main')

一些 Main 实体可以具有完全1:1关系的 Peer 实体(在 Main 实体之后创建)。



我在考虑在创建 Peer 实体时,我可以简单地指定一个数字ID等于相应的 Main 实体的ID(由数据存储自动生成,因此保证是唯一的):

  main_entity = Main(field1 ='blah')
main_entity.put()
$ b $ peer_entity = Peer(id = main_entity.key.id(),field2 = 10)
peer_entity.put()

这样我就可以大大简化应用程序的逻辑, d存储和处理实体密钥以交叉引用这些实体,特别是在跨请求传递它们时。例如,在我有主要实体的情况下,我可以简单地做:

  peer_entity = Peer.get_by_id(main_entity.key .id())

同样,在我有对等实体的上下文中:

  main_entity = Main.get_by_id(peer_entity.key.id())

根据文档键只是(kind,id)对,这意味着只要种类不同而且id是唯一的我不应该有问题。到目前为止(在开发服务器上)完成的测试似乎工作正常。



我的想法是否正确,或者我错过了一些东西(在我的测试中迄今为止还只是幸运的)?

解决方案

我多年来一直都在使用这种方法,并且从来没有任何问题。例如,每次更新时一个实体,每个索引字段也被更新。出于这个原因,我经常将一个复杂的实体分成很少更新的部分和经常更新的部分,并且对于两个实体使用不同的种类,但是具有相同的ID。 AdEntity和AdCounterEntity。这样,正如您正确观察到的那样,应用程序逻辑被简化了,因为您只需记住一个ID即可根据需要检索两个实体。


My (python) app is using several entities, many of them in a 1:1 relationship. For example:

class Main(ndb.Model):
    field1 = ndb.StringProperty()
    #peer_key = ndb.KeyProperty(kind='Peer')

class Peer(ndb.model):
    field2 = ndb.IntegerProperty()
    #main_key = ndb.KeyProperty(kind='Main')

Some of the Main entities may have a Peer entity (created after the Main entity) in exactly a 1:1 relationship.

I was thinking that at creation of the Peer entity I could simply specify a numerical ID equal to the corresponding Main entity's ID (auto-generated by the datastore and thus guaranteed to be unique):

main_entity = Main(field1='blah')
main_entity.put()

peer_entity = Peer(id=main_entity.key.id(), field2=10)
peer_entity.put()

This way I could significantly simplify my app's logic, without the need of storing and processing the entity keys to cross-reference these entities, especially when passing them across requests. For example, in a context where I have the main entity I could simply do:

peer_entity = Peer.get_by_id(main_entity.key.id())

Similarly, in a context where I have the peer entity:

main_entity = Main.get_by_id(peer_entity.key.id())

According to the documentation keys are just (kind, id) pairs, meaning as long as the kinds are different and the ids are unique I shouldn't have problems. The tests I've done so far (on the development server) appear to be working fine.

Is my thinking correct or am I missing something (and was just lucky so far in my testing)?

解决方案

I use this approach all the time for many years, and never had any problems.

For example, every time you update an entity, every indexed field is updated too. For this reason, I often split a complex entity into "rarely updated" part and "frequently updated" part, and use different kinds but the same ID for both entities, e.g. AdEntity and AdCounterEntity. This way, as you correctly observed, the app logic is simplified as you need to remember only one ID to retrieve both entities as necessary.

这篇关于为不同类型的其他实体重新使用实体的ID - 理智的想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆