Handle multiple relationships between two same objects
Bug report
Hi,
Here is a simplified script of what is notworking
var A = thinky.createModel('A', {id: String});
var B = thinky.createModel('B', {id: String});
A.hasAndBelongsToMany(B, 'bs', 'id', 'id');
B.hasAndBelongsToMany(A, 'as', 'id', 'id');
var a = new A({id: 'a1'});
var b = new B({id: 'b1'});
a.addRelation('bs', b);
a.addRelation('bs', b);
a.addRelation('bs', b);
This is not working. The weird thing is that if I make three objects on my own with A_id and B_id it will work (r.table('A_B').insert([ rel, rel, rel ]);) then think knows about it
The short answer is that you cannot add multiple times the same relations, at least that's how hasAndBelongsToMany was built.
What's your use case behind multiple relations? Maybe extra data in the relation would be what you need? Though it's not implemented at the moment...
Example case:
- A Promotion Table
- A Item table
1 beer - $7 3 beer - $18
Promotion n:m Item
Promotion called "3 beer" has 3 times the Item called "Beer".
I tried once adding extra data to relationship. #282 is mine :D. I'll check this if I have time one day o/.
But why would hasAndBelongsToMany supports only one relationship ? (When it supports "without coding effort" if there is three times the same relationship inside ?
The easy way would be to change addRelation to make an entry inside the table with id1_id2 without caring if there is already an entry and then everything would work