如何在GAE ndb中建模一个唯一约束 [英] How to model a unique constraint in GAE ndb

查看:126
本文介绍了如何在GAE ndb中建模一个唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有几个捆绑(Mjbundle),它们基本上是一堆问题(Mjquestion)。 Mjquestion有一个整数索引属性,它必须是唯一的,但它应该只在包含它的包中是唯一的。我不确定如何正确建模这样的东西,我尝试使用下面的结构化(重复)属性来做到这一点,但实际上并没有限制Mjquestion索引的唯一性。什么是更好/正常/正确的做法?

  class Mjquestion(ndb.Model):
这是一个Mjquestion。
index = ndb.IntegerProperty(indexed = True,required = True)
genre1 = ndb.IntegerProperty(indexed = False,required = True,choices = [1 ,2,3,4,5,6,7])
genre2 = ndb.IntegerProperty(indexed = False,required = True,choices = [1,2,3])
#(将添加一些更多的数据属性)

class Mjbundle(ndb.Model):
这是一个Mjbundle。
mjquestions = ndb.StructuredProperty(Mjquestion ,重复= True)
time = ndb.DateTimeProperty(auto_now_add = True)

上面的模型和已经获取了某个Mjbundle实体,我不知道如何从基于索引的mjquestions快速获取Mjquestion。关于结构化属性过滤的解释看起来像它在Mjbundle类型层次上工作,而我已经有了一个Mjbundle实体,并不知道如何只能快速查询该实体所包含的问题,而无需在代码中手动遍历它们。)

所以我愿意提供关于如何这样做更好。




  • 我阅读这个信息的答案: https://stackoverflow.com/a/3855751/129202 它提供了关于可伸缩性的一些想法和相关说明,我将期待只是几个捆绑包,但每个捆绑包都会有问题数千个。

  • 可能我不应该使用Mjbundle的mjquestions属性,而应该关注于父母:创建的每个Mjquestion都应该有一个Mjbundle实体作为父对象。然后手动通过执行祖先查询来在插入时间处实施唯一性。 当您使用StructuredProperty时,所有类型的实体都将作为包含实体的一部分进行存储 - 因此,当您获取您的包时,您已经提取了所有问题。如果你坚持这种存储方式,迭代检查代码是解决方案。


    I want to have several "bundles" (Mjbundle), which essentially are bundles of questions (Mjquestion). The Mjquestion has an integer "index" property which needs to be unique, but it should only be unique within the bundle containing it. I'm not sure how to model something like this properly, I try to do it using a structured (repeating) property below, but there is yet nothing actually constraining the uniqueness of the Mjquestion indexes. What is a better/normal/correct way of doing this?

    class Mjquestion(ndb.Model):
        """This is a Mjquestion."""
        index = ndb.IntegerProperty(indexed=True, required=True)
        genre1 = ndb.IntegerProperty(indexed=False, required=True, choices=[1,2,3,4,5,6,7])
        genre2 = ndb.IntegerProperty(indexed=False, required=True, choices=[1,2,3])
        #(will add a bunch of more data properties later)
    
    class Mjbundle(ndb.Model):
        """This is a Mjbundle."""
        mjquestions = ndb.StructuredProperty(Mjquestion, repeated=True)
        time = ndb.DateTimeProperty(auto_now_add=True)
    

    (With the above model and having fetched a certain Mjbundle entity, I am not sure how to quickly fetch a Mjquestion from mjquestions based on the index. The explanation on filtering on structured properties looks like it works on the Mjbundle type level, whereas I already have a Mjbundle entity and was not sure how to quickly query only on the questions contained by that entity, without looping through them all "manually" in code.)

    So I'm open to any suggestion on how to do this better.

    • I read this informational answer: https://stackoverflow.com/a/3855751/129202 It gives some thoughts about scalability and on a related note I will be expecting just a couple of bundles but each bundle will have questions in the thousands.

    • Maybe I should not use the mjquestions property of Mjbundle at all, but rather focus on parenting: each Mjquestion created should have a certain Mjbundle entity as parent. And then "manually" enforce uniqueness at "insert time" by doing an ancestor query.

    解决方案

    When you use a StructuredProperty, all of the entities that type are stored as part of the containing entity - so when you fetch your bundle, you have already fetched all of the questions. If you stick with this way of storing things, iterating to check in code is the solution.

    这篇关于如何在GAE ndb中建模一个唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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