Search code examples
pythongoogle-app-engineuniquegoogle-cloud-datastore

How to model a unique constraint in GAE ndb


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.


Solution

  • 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.