parent
6a962583b0
commit
ef143dc5eb
|
@ -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;
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue