Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fixes #1389] Problems with Through Associations with custom columnNames #1392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/waterline/model/lib/associationMethods/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,17 @@ Add.prototype.createManyToMany = function(collection, attribute, pk, key, cb) {

// Grab the associated collection's primaryKey
var collectionAttributes = this.collection.waterline.schema[attribute.collection.toLowerCase()];
var associationKey = collectionAttributes.attributes[attribute.on].via;
var associationKey = null;

// If this is a throughTable, look into the meta data cache for what key to use
if (collectionAttributes.throughTable) {
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key];
var cacheKey = collectionAttributes.throughTable[attribute.via + '.' + key];
if (!cacheKey) {
return cb(new Error('Unable to find the proper cache key in the through table definition'));
}

associationKey = cacheKey;
}
}else associationKey = collectionAttributes.attributes[attribute.on].via;

if (!associationKey) {
return cb(new Error('No Primary Key set on the child record you ' +
Expand All @@ -313,7 +313,7 @@ Add.prototype.createManyToMany = function(collection, attribute, pk, key, cb) {
var _values = {};

criteria[associationKey] = pk;
criteria[attribute.onKey] = this.proto[this.primaryKey];
criteria[attribute.on] = this.proto[this.primaryKey];
_values = _.clone(criteria);

async.auto({
Expand Down
6 changes: 3 additions & 3 deletions lib/waterline/model/lib/associationMethods/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,17 @@ Remove.prototype.removeManyToMany = function(collection, attribute, pk, key, cb)

// Grab the associated collection's primaryKey
var collectionAttributes = this.collection.waterline.schema[attribute.collection.toLowerCase()];
var associationKey = collectionAttributes.attributes[attribute.on].via;
var associationKey = null;

// If this is a throughTable, look into the meta data cache for what key to use
if (collectionAttributes.throughTable) {
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key];
var cacheKey = collectionAttributes.throughTable[attribute.via + '.' + key];
if (!cacheKey) {
return cb(new Error('Unable to find the proper cache key in the through table definition'));
}

associationKey = cacheKey;
}
}else associationKey =collectionAttributes.attributes[attribute.on].via;

if (!associationKey) {
return cb(new Error('No Primary Key set on the child record you ' +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
var Waterline = require('../../../lib/waterline'),
assert = require('assert');

describe('Model', function() {
describe('associations Many To Many Through with columnName', function() {
describe('.add() with an id', function() {

/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////

var collections = {};
var prefValues = [];

before(function(done) {
var waterline = new Waterline();

var User = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'person',
attributes: {
preferences: {
collection: 'preference',
via: 'person',
through: 'person_preference'
}
}
});

var Preference = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'preference',
attributes: {
foo: 'string',
people: {
collection: 'person',
via: 'preference',
through: 'person_preference'
}
}
});

var UserPreference = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'person_preference',
attributes: {
person:{
model: 'person',
columnName: 'person_id'
},
preference: {
model: 'preference',
columnName: 'preference_id'
}
}
});


waterline.loadCollection(User);
waterline.loadCollection(Preference);
waterline.loadCollection(UserPreference);

var _values = [
{ id: 1, preferences: [{ foo: 'bar' }, { foo: 'foobar' }] },
{ id: 2, preferences: [{ foo: 'a' }, { foo: 'b' }] },
];

var i = 1;

var adapterDef = {
find: function(con, col, criteria, cb) {
if(col === 'person_preference') return cb(null, []);
cb(null, _values);
},
update: function(con, col, criteria, values, cb) {
if(col === 'preference') {
prefValues.push(values);
}

return cb(null, values);
},
create: function(con, col, values, cb) {
prefValues.push(values);
return cb(null, values);
},
};

var connections = {
'my_foo': {
adapter: 'foobar'
}
};

waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) {
if(err) done(err);
collections = colls.collections;
done();
});
});


/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////

it('should pass foreign key values to update method for each relationship', function(done) {
collections.person.find().exec(function(err, models) {
if(err) return done(err);

var person = models[0];

person.preferences.add(1);
person.preferences.add(2);

person.save(function(err) {
if(err) return done(err);
assert(prefValues.length === 2);

assert(prefValues[0].preference_id === 1);
assert(prefValues[1].preference_id === 2);

done();
});
});
});

});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
var Waterline = require('../../../lib/waterline'),
assert = require('assert');

describe('Model', function() {
describe('associations Many To Many Through with columnName', function() {
describe('.add() with an object', function() {

/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////

var collections = {};
var fooValues = [];

before(function(done) {
var waterline = new Waterline();

var User = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'person',
attributes: {
preferences: {
collection: 'preference',
via: 'person',
through: 'person_preference'
}
}
});

var Preference = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'preference',
attributes: {
foo: 'string',
people: {
collection: 'person',
via: 'preference',
through: 'person_preference'
}
}
});

var UserPreference = Waterline.Collection.extend({
connection: 'my_foo',
tableName: 'person_preference',
attributes: {
person:{
model: 'person',
columnName: 'person_id'
},
preference: {
model: 'preference',
columnName: 'preference_id'
}
}
});

waterline.loadCollection(User);
waterline.loadCollection(Preference);
waterline.loadCollection(UserPreference);

var _values = [
{ id: 1, preferences: [{ id: 1, foo: 'bar' }, { id: 2, foo: 'foobar' }] },
{ id: 2, preferences: [{ id: 3, foo: 'a' }, { id: 4, foo: 'b' }] },
];

var i = 1;
var added = false;

var adapterDef = {
find: function(con, col, criteria, cb) {
if(col === 'person_preference') {
if(!added) return cb();
if(criteria === fooValues[0]) return cb(null, fooValues[0]);
return cb(null, []);
}

return cb(null, _values);
},
create: function(con, col, values, cb) {
if(col !== 'person_preference') {
values.id = i;
i++;
return cb(null, values);
}

added = true;
fooValues.push(values);
return cb(null, values);
},
update: function(con, col, criteria, values, cb) { return cb(null, values); }
};

var connections = {
'my_foo': {
adapter: 'foobar'
}
};

waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) {
if(err) done(err);
collections = colls.collections;
done();
});
});


/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////

it('should pass model values to create method for each relationship', function(done) {
collections.person.find().exec(function(err, models) {
if(err) return done(err);

var person = models[0];

person.preferences.add({ foo: 'foo' });
person.preferences.add({ foo: 'bar' });

person.save(function(err) {
if(err) return done(err);

assert(fooValues.length === 2);

assert(fooValues[0].preference_id === 1);
assert(fooValues[0].person_id === 1);

assert(fooValues[1].preference_id === 2);
assert(fooValues[1].person_id === 1);

done();
});
});
});

});
});
});
Loading