Custom Table Names on rels (#1303)

hasAndBelongsToMany relation
This commit is contained in:
Waldemar Zahn 2017-04-05 18:42:21 +02:00 committed by Sakib Hasan
parent 6a962583b0
commit ef143dc5eb
2 changed files with 25 additions and 4 deletions

View File

@ -1554,11 +1554,16 @@ RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom,
if (!params.through) {
if (params.polymorphic) throw new Error(g.f('{{Polymorphic}} relations need a through model'));
if (params.throughTable) {
params.through = modelFrom.dataSource.define(params.throughTable);
} else {
var name1 = modelFrom.modelName + modelTo.modelName;
var name2 = modelTo.modelName + modelFrom.modelName;
params.through = lookupModel(models, name1) || lookupModel(models, name2) ||
modelFrom.dataSource.define(name1);
}
}
var options = {as: params.as, through: params.through};
options.properties = params.properties;

View File

@ -2507,6 +2507,22 @@ describe('relations', function() {
});
});
});
it('should use author_pictures as modelThrough', function(done) {
Author.hasAndBelongsToMany(Picture, {throughTable: 'author_pictures'});
Author.relations['pictures'].toJSON().should.eql({
name: 'pictures',
type: 'hasMany',
modelFrom: 'Author',
keyFrom: 'id',
modelTo: 'Picture',
keyTo: 'authorId',
multiple: true,
modelThrough: 'author_pictures',
keyThrough: 'pictureId',
});
done();
});
});
describe('belongsTo', function() {