user7254823
user7254823

Reputation:

GAE: What's the best practice for structuring models datastore

So I have been using Relational databases for the past 3 years and I'm having some troubles with the mindset of creating noSql databases. I have the following Models:

class Account(ndb.Model):
    first_name = ndb.StringProperty()
    last_name = ndb.StringProperty()
    email = ndb.StringProperty()
    password = ndb.StringProperty()


class Organisation(ndb.Model):
    org_name = ndb.StringProperty()
    org_address = ndb.StringProperty()
    .. and so on

class UserProfile(ndb.Model):
    user = ndb.KeyProperty(kind='Account')
    organisation = ndb.KeyProperty(kind='Organisation')
    role = ndb.StringProperty()

Account x Organisation is technically a many to many relationship so UserProfile is acting like a link table, like in relational databases

From my understanding, I shouldn't have to do more than 1 query in order to get information about an entity. For example, if I query to get all UserProfiles belonging to the user, then I shouldn't have to loop through and query each UserProfiles entity to get the information about each organisation individually. I feel the way I have structured this isn't the best or the way it should be.

Another thing I was thinking of is to store the organisation details inside of the Account model itself, but then if the organisation details changed, then I would have to query all the accounts connected to the organisation and update each of them individually.

Just generally confused on how to do this, any help or explanation of where I am going wrong would be appreciated.

Upvotes: 0

Views: 59

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39814

I shouldn't have to do more than 1 query in order to get information about an entity

Well, in your example you're not - the additional lookups (not queries) would be for gathering information about organisations, which are different entities than the user profiles entities you queried for.

But you got the 2 general (and opposite) ideas:

  • use ndb.KeyProperty for referencing other entities when modelling 1-to-many and many-to-many relationships: minimal/no duplicate information so no sync/maintenance required, but requiring multiple queries/lookups to aggregate information spread across multiple entities

  • clone desired info right inside the respective entities (maybe use Structured Properties?) - allowing faster access (no additional queries/lookups required to get the info), but sync/maintenance required when the cloned info needs to be updated.

It's really up to you to decide what makes sense for you application and to strike a balance/middle ground between the 2 extremes accordingly.

Personally I tend to favour the 1st one, if possible. And when I face cases in which multiple queries/lookups appear needed I always double-check if using simultaneously info from multiple entities is really needed (maybe it's acceptable to display the organisation details in a separate screen, for example?). But that's a personal preference only.

Upvotes: 1

Related Questions