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

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

问题描述

我在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?

注意:连接的注册和更新是正确的,只有未传递到表的其他属性.

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

FunctionalPrivilege模型:

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

方法创建:

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

它应该适用于已经存在的特权.它不适用于实体数组(在您的情况下为特权),因此我不得不多次调用"add"方法.像这样:

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

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

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