Sequelize belongsToMany 连接表中的附加属性 [英] Sequelize belongsToMany additional attributes in join table

查看:17
本文介绍了Sequelize belongsToMany 连接表中的附加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 belongsToMany 关系的连接表中的一个附加属性有疑问.在 set 或 add 方法中,此属性未传递给 mysql.

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.

我在 set 方法中将文档传递为通过"属性,但它不起作用.由于遵循文档不起作用,有人知道可能出现什么问题吗?

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?

注意:join的注册和更新是正确的,只是没有传递给表的附加属性.

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

功能模型:

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;
}

特权模型:

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;
}

功能特权模型:

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;
}

方法创建:

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

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

推荐答案

我没能用 'set' 函数做到这一点,但它用 'add' 方法对我有用:

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 }}));
)

这篇关于Sequelize belongsToMany 连接表中的附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆