parent
6a962583b0
commit
ef143dc5eb
|
@ -1554,10 +1554,15 @@ RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom,
|
||||||
|
|
||||||
if (!params.through) {
|
if (!params.through) {
|
||||||
if (params.polymorphic) throw new Error(g.f('{{Polymorphic}} relations need a through model'));
|
if (params.polymorphic) throw new Error(g.f('{{Polymorphic}} relations need a through model'));
|
||||||
var name1 = modelFrom.modelName + modelTo.modelName;
|
|
||||||
var name2 = modelTo.modelName + modelFrom.modelName;
|
if (params.throughTable) {
|
||||||
params.through = lookupModel(models, name1) || lookupModel(models, name2) ||
|
params.through = modelFrom.dataSource.define(params.throughTable);
|
||||||
modelFrom.dataSource.define(name1);
|
} 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};
|
var options = {as: params.as, through: params.through};
|
||||||
|
|
|
@ -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() {
|
describe('belongsTo', function() {
|
||||||
|
|
Loading…
Reference in New Issue