Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com many-to-many with attributes

I have a many-to-many relation between "Ingredient" and "Recipe". I know I can add and remove object from the relation using

var Recipe = Parse.Object.extend("Recipe");
var Ingredient = Parse.Object.extend("Ingredient");

var recipe = new Recipe();
var ingredient = new Ingredient();

ingredient.id = "abcDef1234"; //random

var recipe_ingredients = recipe.relation("recipe_ingredients");
recipe_ingredients.add(ingredient);

But this only adds the ingredient. How do I also store a field like "quantity" along with each ingredient added? (Like extra attributes on a pivot table?)

Thanks!

like image 681
Anindit Karmakar Avatar asked Mar 14 '26 04:03

Anindit Karmakar


1 Answers

You can't. This is one of Parse's many limitations. You'll have to abandon the builtin relations model, create your own join table. That is, create a new object type which has two pointers: the first, the source object; the second, the target. This forms the relation. You can then add fields on this table to act as attributes on the relationship. If you want, you can keep a pointer from the object that originally held the relation, targeting the join table entry.

Of course, once you realise that this doubles the number of requests you're using (each database hit is a "request" apparently), then you start to realise that the Parse database model coupled with their charging model scales about as well as building skyscrapers from matchsticks.

like image 183
Adam Wright Avatar answered Mar 17 '26 04:03

Adam Wright



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!