本文介绍了App Engine:结构化属性与参考属性的一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计数据存储的背景来自于iOS上的Core Data,它支持与另一个实体具有一对多关系的属性。



我在工作目前有三种实体类型的App Engine专案:




  • 使用者代表使用应用程式的使用者。

  • 专案代表一个专案。 用户可能与许多项目相关联。

  • 帖子 Project 后面的主要内容。 项目可能有很多帖子。



目前,用户有一个属性, ,这是与 Project 实体的一对多关系。 项目有一个属性 posts ,这是一个一对多的关系 实体。



在这种情况下,Datastore的参考资源或NDB的结构化属性更适合该作业(两个概念上如何不同)?有没有更好的方式来构建我的数据?

解决方案

通过引用属性你可能意味着Key Property。这是对另一个数据存储区实体的引用。它存在于db和ndb API中。使用这些,您可以通过将许多实体指向另一个实体的键来建模多对一关系。



结构化属性是一个完全不同的野兽。



这里有一个文档的示例,其中包含一个联系人的多个地址:

p>

  class Address(ndb.Model):
type = ndb.StringProperty()#Eg,'home','work '
street = ndb.StringProperty()
city = ndb.StringProperty()

class Contact(ndb.Model):
name = ndb.StringProperty
addresses = ndb.StructuredProperty(Address,repeated = True)

guido = Contact(name ='Guido',
addresses = [Address(type ='home',
city ='Amsterdam'),
地址(type ='work',
street ='Spear St',
city ='SF')])
b $ b guido.put()

对于你的特定应用,我建议使用NDB最好使用最新版本的api可用),并具有以下功能:



Post模型包含在项目模型下作为重复的结构化属性。
用户包含一个重复的KeyProperty,其中包含他们有权限的项目的键。



为了使它更复杂,您可以创建另一个模型代表项目和权限/角色,然后将其作为用户模型中的重复结构化属性。



您想挂在键上的主要原因是根据HRD的最终一致性保持数据的可访问性。



如果您需要更多帮助,请告诉我们。



编辑:



以下是建议的结构:






  • 用户

  • 用户项目映射(可选,需要处理权限)

  • 项目

  • 发布



用户模型应包含User-Project-Mapping重复的结构化属性。



项目模型应包含重复的结构化属性。



需要包含项目的关键引用和相关的权限表示。



由于这听起来像一个商业项目,如果你想进一步的帮助,我会很高兴咨询你。希望你有足够的成功!


My background with designing data stores comes from Core Data on iOS, which supports properties having a one-to-many relationship with another entity.

I'm working on an App Engine project which currently has three entity types:

  • User, which represents a person using the app.
  • Project, which represents a project. A User may be associated with many projects.
  • Post, which is the main content behind a Project. A Project may have many posts.

Currently, User has a property, projects, that is a one-to-many relationship to Project entities. Project has a property, posts, that is a one-to-many relationship to Post entities.

In this case, is Datastore's Reference Property or NDB's Structured Property better for the job (and how are the two conceptually different)? Is there a better way to structure my data?

解决方案

By reference property you probably mean Key Property. This is a reference to another datastore entity. It is present in both db and ndb APIs. Using these, you can model a many to one relationship by pointing many entities to the key of another entity.

Structured property is a completely different beast. It allows you to define a data structure, and then include it within another entity.

Here's an example from the docs where you include multiple addresses for a single contact:

class Address(ndb.Model):
  type = ndb.StringProperty() # E.g., 'home', 'work'
  street = ndb.StringProperty()
  city = ndb.StringProperty()

class Contact(ndb.Model):
  name = ndb.StringProperty()
  addresses = ndb.StructuredProperty(Address, repeated=True)

guido = Contact(name='Guido',
                addresses=[Address(type='home',
                                   city='Amsterdam'),
                           Address(type='work',
                                   street='Spear St',
                                   city='SF')])

guido.put()

For your specific application I'd recommend using NDB (it's always best to use the latest version of the api available), with the following:

Post model included under Project model as a repeated structured property.Users include a repeated KeyProperty that contains the keys of the Projects they have permissions to.

To make it a bit more complex, you can create a another model to represent projects and permissions/roles, and then include that as a repeated structured property within the user model.

The main reason you want to hang on to the keys, is to keep the data accessible in light of HRDs eventual consistency.

Let me know if you need any more help on this.

EDIT:

To clarify, here's the proposed structure:

Models:

  • User
  • User-Project-Mapping (optional, needed to handle permissions)
  • Project
  • Post

User model should contain User-Project-Mapping as repeated structured property.

Project model should contain Post as repeated structured property.

User-Project-Mapping only needs to contain Key reference to the Project and relevant permissions representation.

Since this sounds like a commercial project, if you'd like further help with this, I'll gladly consult for you. Hope you have enough to succeed!

这篇关于App Engine:结构化属性与参考属性的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:06