One-to-many relationship in ndb

I read the engine in a Google application and prepare a sample to better understand it.

In a nutshell, the user can record an entry for each day of the month, like a calendar. And the user can view the entries monthly. Thus, no more than 30 um at a time.

I originally used db , and the one-to-many relationship was straightforward.

But as soon as I stumbled upon ndb , I realized that there are two ways to model a one-to-many relationship.

1) The structured property seems to act as a repetitive property in the User model. Does this mean that if I get one user, I would automatically receive all the entries that she entered? (like a whole year) It's not very effective, right? I assume that the advantage is that you get all the associated data in a single read operation.

  from google.appengine.ext import ndb class User(UserMixin, ndb.Model): email = ndb.StringProperty(required = True) password_hash = ndb.TextProperty(required = True) record = ndb.StructuredProperty(Record, repeated=True) class Record(ndb.Model): notes = ndb.TextProperty() 

2) As an alternative, I could use a perhaps more classic way:

  class User(UserMixin, ndb.Model): email = ndb.StringProperty(required = True) password_hash = ndb.TextProperty(required = True) class Record(ndb.Model): user = ndb.KeyProperty(kind=User) notes = ndb.TextProperty() 

Which method is better in my use case?

+4
source share
1 answer

The disadvantage of using StructuredProperty instead of KeyProperty is that with StructuredProperty, the restriction on the total size of an entity (1MB) applies to the sum of the User data and all the records it contains (since structured properties are serialized as part of the User object), with KeyProperty each record has a 1 MB limit .

+8
source

Source: https://habr.com/ru/post/1487032/


All Articles