不要怂,就是干,撸起袖子干!

Commit 47c040a5 by Jan Aagaard Meier

Fix through table attributes not working for createAssociation in n:m. Closes #2155

1 parent 1d024baa
......@@ -109,7 +109,7 @@ var options = {
if (!javadoc.name) {
var property = getTag(javadoc.raw.tags, 'property');
if (property) {
javadoc.name = property.string;
javadoc.name = property.types[0];
}
}
if (javadoc.isMixin) {
......
......@@ -449,21 +449,16 @@ module.exports = (function() {
var association = this;
obj[this.accessors.create] = function(values, fieldsOrOptions) {
var instance = this
, options = {};
var instance = this;
if (values === undefined) {
values = {};
}
if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
options.transaction = fieldsOrOptions.transaction;
}
if (Object(association.through) === association.through) {
// Create the related model instance
return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) {
return instance[association.accessors.add](newAssociatedObject, options).return(newAssociatedObject);
return instance[association.accessors.add](newAssociatedObject, fieldsOrOptions).return(newAssociatedObject);
});
} else {
values[association.identifier] = instance.get(association.source.primaryKeyAttribute);
......
......@@ -136,6 +136,7 @@ Hooks.hook = function() {
* @param {String} hooktype
* @param {String} [name] Provide a name for the hook function. This serves no purpose, other than the ability to be able to order hooks based on some sort of priority system in the future.
* @param {Function} fn The hook function
*
* @alias hook
*/
Hooks.addHook = function(hookType, name, fn) {
......@@ -196,7 +197,8 @@ Hooks.afterCreate = function(name, fn) {
* A hook that is run before destroying a single instance
* @param {String} name
* @param {Function} fn A callback function that is called with instance, callback(err)
* alias beforeDelete
*
* @alias beforeDelete
*/
Hooks.beforeDestroy = function(name, fn) {
Hooks.addHook.call(this, 'beforeDestroy', name, fn);
......@@ -206,6 +208,7 @@ Hooks.beforeDestroy = function(name, fn) {
* A hook that is run after destroying a single instance
* @param {String} name
* @param {Function} fn A callback function that is called with instance, callback(err)
*
* @alias afterDelete
*/
Hooks.afterDestroy = function(name, fn) {
......
......@@ -1083,6 +1083,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
return this.t.rollback();
});
});
it('supports setting through table attributes', function () {
var User = this.sequelize.define('user', {})
, Group = this.sequelize.define('group', {})
, UserGroups = this.sequelize.define('user_groups', {
isAdmin: Sequelize.BOOLEAN
});
User.hasMany(Group, { through: UserGroups });
Group.hasMany(User, { through: UserGroups });
return this.sequelize.sync({ force: true }).then(function () {
return Group.create({});
}).then(function (group) {
return Promise.join(
group.createUser({ id: 1 }, { isAdmin: true }),
group.createUser({ id: 2 }, { isAdmin: false }),
function () {
return UserGroups.findAll();
}
);
}).then(function (userGroups) {
expect(userGroups[0].userId).to.equal(1);
expect(userGroups[0].isAdmin).to.be.ok;
expect(userGroups[1].userId).to.equal(2);
expect(userGroups[1].isAdmin).not.to.be.ok;
});
});
});
describe('addAssociations', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!