1

I use Sails@beta 0.10.0-rc5, I upgraded to rc7 and it my unit tests failed on Many-To-Many association!

I've Many-To-Many relation between 'Center' & 'Teacher' models.

** I ADDED DEBUGGING CONCLUSIONS BELOW **

CenterController.js:

sails.log.info("before remove teachers: ", center.teachers);
sails.log.info("removing teacher = '", req.param('teacher_id'));
center.teachers.remove(req.param('teacher_id'));
sails.log.info("after remove teachers: ", center.teachers);
center.save(function(err) {if(err)... sails.log.info("IN SAVE FUNCTION"); ...});

OUTPUT:

before remove teachers: [ { id: '537f19dbeb09ff341598c77b' },
add: [Function: add],
remove: [Function: remove] ]
removing teacher = 537f19dbeb09ff341598c77b
after remove teachers: [ { id: '537f19dbeb09ff341598c77b' },
add: [Function: add],
remove: [Function: remove] ]

** DEBUG CONCLUSIONS**

waterline\lib\waterline\model\lib\associationMethods\remove.js

// line 240
// Build up criteria and updated values used to create the record 
var criteria = {};
criteria[associationKey] = pk;
criteria[attribute.on] = this.proto[this.primaryKey];
console.log('criteria: ', criteria);

OUTPUT:

criteria: { teacher_centers: '537f24b98e64e7fc1de20718',
center_teachers: '537f19dbeb09ff341598c77b' }

Problem (my guess):

The last output from remove.js is printed after the "IN SAVE FUNCTION" text from center.save().

Any idea?

user2867106
  • 1,139
  • 1
  • 13
  • 31
  • What is your question? Is the teacher not being removed from the center when it is saved? Note that calling `remove` on an instance does not actually remove the associated model from that instance; it just sets up criteria internally to persist the removal when `.save()` is called. – sgress454 May 23 '14 at 16:04
  • yes, the teacher is not removed from the collection (it works in rc5!). even after calling save(), the teacher is not being removed. It was an open issue that was fixed in rc5, but now arrived again. Again, if I downgrade to rc5, the same code passes. – user2867106 May 23 '14 at 16:35
  • moreover, i am doing it exactly as the documentation here: https://github.com/balderdashy/waterline-docs/blob/master/associations.md – user2867106 May 23 '14 at 16:41
  • I got your point, but still, save() is not removing the teacher from the center as it did in rc5. – user2867106 May 23 '14 at 16:52

1 Answers1

0

Looks like this was a bug in Waterline, as detailed in this report. Thanks for the report and for your support--the bug has been patched and a new RC should be pushed soon. In the meantime you can either apply the patch yourself by changing line 104 in lib/waterline/model/lib/defaultMethods/save.js of Waterline RC4 to:

addAssociations: ['buildAssociationOperations', 'updateRecord', function(next, results) {

or check out the master branch of Waterline from https://github.com/balderdashy/waterline.git and replace the copy that exists in your Sails install's node_modules folder.

sgress454
  • 24,870
  • 4
  • 74
  • 92