Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many-To-Many Relationship in ndb

Trying to model a many-to-many relationship with ndb. Can anyone point to a good example of how to do this?

At here is an example of what I have at the moment:

class Person(ndb.Model):
     guilds = ndb.KeyProperty(kind="Guild", repeated=True)

class Guild(ndb.Model)
     members = ndb.KeyProperty(kind="Person", repeated=True)

     def add_person(self, person):
         self.members.append(person.key)
         self.put()
         person.guilds.append(self.key)
         person.put()

Is this the correct way to go about it? I have had a good look around but can't seem to find any good documentation on the subject.

In the datastore viewer, I can see this relationship being stored as a list of Keys, which I expect.

However, when I try to use them in the Person class methods like this:

for guild in self.guilds:

I get:

TypeError: 'KeyProperty' object is not iterable
like image 239
chrisw Avatar asked Jun 24 '14 16:06

chrisw


People also ask

What is a many-to-many relationship in database?

A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table. For example, a many-to-many relationship exists between employees and projects: employees can work on various projects, and a project can have many employees working on it.

What is many to many relationship in data modeling?

Many to Many Relationships In the data modeling world there are several types of relationships between entities. In this article we will tell you more about the many-to-many relationship type. A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table.

How many one-to-many relationships are added to relate the tables?

Two one-to-many relationships are added to relate the tables. Here's an updated model diagram of the related tables. A fact-type table named Transactionhas been added.

How do you convert many-to-many relationships to one to many relationships?

For efficient processing, you can convert the many-to-many relationship tables into two one-to-many relationships by connecting these two tables with an intersection table that contains the keys of both tables. Our data modeling tool DeZign for Databases, automatically resolves many-to-many relationships.


1 Answers

No. That is not the correct way to go about it.

You can model a many-to-many relationship with only one repeated property:

class Person(ndb.Model):
    guilds = ndb.KeyProperty(kind="Guild", repeated=True)

class Guild(ndb.Model):
    @property
    def members(self):
        return Person.query().filter(Person.guilds == self.key)

    def add_person(self, person):
        person.guilds.append(self.key)
        person.put()

or viceversa with:

class Person(ndb.Model):
    @property
    def guilds(self):
        return Guild.query().filter(Guild.members == self.key)

class Guild(ndb.Model):
    members = ndb.KeyProperty(kind="Person", repeated=True)

    def add_person(self, person):
        self.members.append(person.key)
        self.put()

The direction of the relationship depends on many factors (your business model, number of guilds per person, number of members per guild, etc.)

like image 56
Hernán Acosta Avatar answered Oct 19 '22 13:10

Hernán Acosta