3

I have an Entity which has a variable amount of another Entity in it (so I'm usingStructured Property, repeated=True), but that one property can hold variable amount of single entity kind as well. So my code looks like this:

class Property(ndb.Model):
    name    = ndb.StringProperty()
    cost    = ndb.FloatProperty()
    type    = ndb.StringProperty()

class SpecialProperty(ndb.Model):
    name       = ndb.StringProperty()
    properties = ndb.StructuredProperty(Property, repeated=True)
    type       = ndb.StringProperty() 

class Hotel(ndb.Model):
    specialProperties  = ndb.StructuredProperty(SpecialProperty, repeated=True)

But when I try this GAE throws an error. "TypeError: This StructuredProperty cannot use repeated=True because its model class (SpecialProperty) contains repeated properties (directly or indirectly)."

So how can I bypass this? I really need to have this flexible structure.

Big thanks in advance.

2 Answers 2

8

Although a StructuredProperty can be repeated and a StructuredProperty can contain another StructuredProperty, beware: if one structured property contains another, only one of them can be repeated. A work-around is to use LocalStructuredProperty, which does not have this constraint (but does not allow queries on its property values).

https://developers.google.com/appengine/docs/python/ndb/properties#structured

With LocalStructuredProperty you will have the same structure, but you will not be able to filter by this properties. If you really need to do queries by one of this properties -- try put it into another entity.

3

You can't put a repeated StructuredProperty inside another repeated StructuredProperty.

You should use another type of relationship (association, ancestors, etc). For example:

class Property(ndb.Model):
    name    = ndb.StringProperty()
    cost    = ndb.FloatProperty()
    type    = ndb.StringProperty()

class SpecialProperty(ndb.Model):
    hotel      = ndb.KeyProperty(Hotel)
    name       = ndb.StringProperty()
    properties = ndb.StructuredProperty(Property, repeated=True)
    type       = ndb.StringProperty() 

class Hotel(ndb.Model):
    # ... hotel properties

Other option: If you need transactions, you can make Hotel parent of SpecialProperty and Property.

Other option: if you don't need to query on Property, you can store it in a JSONProperty.

4
  • This is a great adive. Thank you! But I would use LocalStructuredProperty in this project
    – momijigari
    Feb 22, 2014 at 13:58
  • 1
    Welcome back to relational database. What I see is that app engine does not support nesting properties more than 1 level. I would recommend people to design model relational in the first place to avoid this constraint. May 19, 2015 at 9:39
  • @MaxTsepkov If you can't query on repeated subobject values, then it seems your observation is essentially correct. It would be good to find a lengthier discussion of this subject. Unfortunately I all find are discussions of trivial examples. Maybe what we need is a Google ZopeEngine! Jul 28, 2017 at 8:01
  • @MaxTsepkov So how could you make nested structured properties with a relational database? I don't think it's possible either. Aug 1, 2017 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.