Sequelize belongsToMany additional attributes in join table

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

I'm having problem with an additional attribute in the join table of the belongsToMany relation. In the set or add method this attribute is not being passed to mysql.

I'm following the documentation pass as "through" the attribute within the set method, but it is not working. Would anyone know what could be wrong since following the documentation is not working?

Note: The registration and update of the join is correct, only the additional attribute that is not being passed to the table.

Functionality Model:

export default function(sequelize, DataTypes) {   const Functionality = sequelize.define('functionality', {     id: {       type: DataTypes.INTEGER(11),       allowNull: false,       primaryKey: true,       autoIncrement: true     },     name: {       field: 'name',       type: DataTypes.STRING(300),       allowNull: false     }   }, {     classMethods: {       associate: function(models) {         Functionality.belongsToMany(models.privilege, { as: 'privilegies', through: models.functionality_privilege, foreignKey: 'functionality_id' });       }     },     tableName: 'functionality',     freezeTableName: true,     timestamps: true,     createdAt: 'createdAt',     updatedAt: 'updatedAt'   });   return Functionality; } 

Privilege Model:

export default function(sequelize, DataTypes) {   const Privilege = sequelize.define('privilege', {     id: {       type: DataTypes.INTEGER(11),       allowNull: false,       primaryKey: true,       autoIncrement: true     },     name: {       field: 'name',       type: DataTypes.STRING(300),       allowNull: false     }   }, {     classMethods: {       associate: function(models) {         Privilege.belongsToMany(models.functionality, { as: 'functionalities', through: models.functionality_privilege, foreignKey: 'privilege_id' });       }     },     tableName: 'privilege',     freezeTableName: true,     timestamps: true,     createdAt: 'createdAt',     updatedAt: 'updatedAt'   });   return Privilege; } 

FunctionalityPrivilege Model:

export default function(sequelize, DataTypes) {   const Functionalityprivilege = sequelize.define('functionality_privilege', {     id: {       type: DataTypes.INTEGER(11),       allowNull: false,       primaryKey: true,       autoIncrement: true     },     edit: {       field: 'edit',       type: DataTypes.BOOLEAN     }   }, {     tableName: 'functionality_privilege',     freezeTableName: true,     timestamps: true,     createdAt: 'created_at',     updatedAt: 'updated_at'   });   return Functionalityprivilege; } 

Method Create:

create(options) {     let obj = options.payload;     return this.functionalityDao.create(obj)     .then((result) => {       return result.setPrivilegies(obj.privilegies, { through: { edit: obj.permissions }})     });   } 

OR

return result.setPrivilegies(obj.privilegies, { through: { edit: true }}) 

回答1:

I didn't manage to do this with 'set' function but it worked for me with 'add' method:

result.addPrivilege(privilege, { through: { edit: true }}); 

It should work for the already existing privilege. It didn't work with the array of entities (privileges in your case), so I had to call 'add' method several times. Like this:

return Promise.all(   privileges.map(privilege =>  result.addPrivilege(privilege, { through: { edit: true }})); ) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!