Mauricio
Mauricio

Reputation: 29

How to populate many to many relationship

I have a MANY-to-MANY relation between two models. But I cannot figure out how to create a form or table to add records to a many to many relation. I am using Google Drive Tables.

I tried to see if the relation is well set, I exported the data and manually populated the keys in the sheet created for the many to many relation. I checked using dropdowns to filter data and it works well.

This is probably basic but I just can't find out how to do it. Please help.

Mauricio

Upvotes: 1

Views: 1508

Answers (1)

Pavel Shkleinik
Pavel Shkleinik

Reputation: 6347

With many-to-many relation on the object/api level App Maker gives an array of related records. Let's say we have 'Questions' and 'Tags' models with many-to-many relation. We can create association from any end of the relation:

// create association from question side
question.Tags.push(tag);

// create association from tag side
tag.Questions.push(question);

Multiselect Widget will do this work for. Let's say we need to add some tags for a questions and we need to bind multiselect to all tags we have in our DB then binding will look similar to this

// binding for Multiselect's names (.. - two dots mean projection)
@datasources.Tags.items..Name

// binding for Multiselect's options
@datasources.Tags.items

// binding for Multiselect's values
// assuming that parent widget is bound to datasource with question
// and `@datasource.item` is question
@datasource.item.Tags

with Suggest Box and Dropdown widgets binding will be similar to multiselect's one but you'll need to do some scripting:

// onValueEdit event handler
// assuming that parent widget is bound to datasource with question
widget.datasource.item.Tags.push(newValue);

Upvotes: 2

Related Questions