ndb设计多对多与父子更改

时间:2015-04-13 13:23:24

标签: python google-app-engine google-cloud-datastore data-modeling app-engine-ndb

经过大量的研究,对我来说很多阅读here在某些情况下仍然不清楚父母和孩子是如何工作的:

 +------------------------+                                    +------------------------+
 | Room Model             |           +----------------+       | Room Model             |
 |                        <-----------+                +------->                        |
 +--+----------------+----+           |  Supporter     |       +------------------------+
    |                |                +----------------+          |                |
    |         +------v-----+                                      |         +------v-----+
    |         |            |                                      |         |            |
    |         | customer   |                                      |         | customer   |
    |         |            |                                      |         |            |
    |         +--+---------+                                      |         +--+---------+
    |            |                                                |            |
    |            |                                                |            |
 +--v+-----------v-+                                           +--v+-----------v-+
 |                 |                                           |                 |
 |   Device        |                                           |   Device        |
 |                 |                                           |                 |
 +---+-----------+-+                                           +---+-----------+-+
     |           |                                                 |           |
     |           |                                                 |           |
     |     +-----v------------+                                    |     +-----v------------+
     |     |                  |                                    |     |                  |
     |     | Issues / tickets |                                    |     | Issues / tickets |
     |     +------------------+                                    |     +------------------+
     |                                                             |
    +v---------------+  +--------+                                +v---------------+  +--------+
    | pdf / pics     |  |Blob_key|                                | pdf / pics     |  |Blob_key|
    |                |  |        |                                |                |  |        |
    +----------------+-->--------+                                +----------------+-->--------+

以下是我的情况:

我有不同的房间和房间,模型是关系中最高的实体,它不会改变,但所有与之相关的孩子都会改变,其中一些孩子会有更多的孩子。因此,一个房间可以同时有几个顾客和几个设备,其中一些属于房间,一些设备分配给房间:

class Room(MainModel):
  # room_number is ID or key of room which is the parent of all entities
  room_number = ndb.StringProperty()
  # I don't know if this is the correct way to do this.
  devices = ndb.KeyProperty(kind='Device', repeated=True)
  # maybe this should be in the Device model?
  devices_allocated = ndb.KeyProperty(kind='Device.allocation', repeated=True)
  based_supporters = ndb.KeyProperty(kind='Supporter')
  supporter = ndb.KeyProperty(kind='Supporter')
  customers = ndb.KeyProperty(kind='Customers', repeated="True")
  # Or should I go for ndb.KeyProperty(kind='Issue', repeated="True")?
  issues = ndb.KeyProperty(kind='Vehicle.issues', repeated="True")

然后我有支持者照顾房间内容(客户和设备),他们可以支持超过1个房间,因此有几个客户和设备(房间的孩子),并有一个基地房间,意味着他们坐着在监控其他房间的房间里......

class Supporter(MainModel):
          # id is ID or key of supporter which is child of room but becomes child of another room if changed
          id = ndb.StringProperty()
          name = ndb.StringProperty()
          #I wonder if is possible to get these entities from the room model... Like this
          devices = ndb.KeyProperty(kind='Room.devices', repeated=True)
          customers = ndb.KeyProperty(kind='Customer', repeated=True)
          #room which supporter stays
          base_room = ndb.KeyProperty(kind='Room')
          #rooms which supporter supports
          rooms = ndb.KeyProperty(kind='Room', repeated="True")

然后我有客户,他们在一个房间(父母),可以使用一个设备(房间的孩子)应该可以根据情况更换,客户可以更换房间,因此房间的设备:

class Customer(MainModel):
      # id is ID or key of customer which is child of room but becomes child of another room if changed
      id = ndb.StringProperty()
      name = ndb.StringProperty()
      # device customer is using which can change
      device = ndb.KeyProperty(kind='Device')
      supporter = ndb.KeyProperty(kind='Supporter')
      #customer room which can change
      room = ndb.KeyProperty(kind='Room')

然后我有总是在房间(父母)或去另一个房间(父母改变)的设备,设备有文件,图片(设备的孩子)随设备移动,它到达另一个房间,例外问题(设备的孩子和房间的间接孩子)属于设备,但应显示它创建的房间号。

class Device(MainModel):
          # id is ID or key of device which is child of room but becomes child of another room if changed
          id = ndb.StringProperty()
          name = ndb.StringProperty()
          customer = ndb.KeyProperty(kind='Customer')
          supporter = ndb.KeyProperty(kind='Supporter')
          # device base room
          base_room =  ndb.KeyProperty(kind='Room')
          # device actual room which can change 
          room = ndb.KeyProperty(kind='Room')
          issue = ndb.KeyProperty(kind="issue", repeated="True")
          file = ndb.KeyProperty(kind="File", repeated="True")
          pics = ndb.KeyProperty(kind="Pics", repeated="True")

最后,我有来自设备的问题和门票(门票和问题的父母)

class Issue(MainModel):
              # id is ID or key of issue which is child of device
              id = ndb.StringProperty()
              title = ndb.StringProperty()
              # This is the parent of the issue model
              device = ndb.KeyProperty(kind='device')
              # place the issue were created
              room = ndb.KeyProperty(kind='Room')
              # issue child's attachments not the same as device attachments
              file = ndb.KeyProperty(kind="File", repeated="True")
              pics = ndb.KeyProperty(kind="Pics", repeated="True")

这是应用密钥属性的正确方法吗?看起来很混乱......特别是在这种复杂的关系设计模型中 你在想什么?我有另一种方法吗?如何动态更改父母或子女或两者的最佳或更有效的方法?

我会尝试一下,我会继续更新,请随时分享意见,为什么以及如何更好或不是。

此致

0 个答案:

没有答案