Sequelize 关联删除问题
发布于 5 年前 作者 zhang962976642 3456 次浏览 来自 问答

各位,在做项目期间,碰到一个问题,不知道如何解决,需求如下: 管理员删除用户角色数据同时,删除角色表关联Api接口表的关联表数据。 目前使用Sequelize,通过官网docs,没有找到相关的demo,查到有个关联方法role.setapis([])的方法删除关联属性。 但是我这边一直在报错提示:role.setapis is not a function
角色模型

module.exports = function (sequelize, DataTypes) {
  const role = sequelize.define("role", {
    id: {
      field: "id",                                    // 别名
      type: DataTypes.UUID,                           // 类型
      allowNull: true,
      unique: true,                                   // 是否是唯一值
      comment: "角色id字段",                           // 简介说明
      primaryKey: true,                               // 是否为主键
      defaultValue: DataTypes.UUIDV1
    },
    mark: {
      field: "mark",
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: "角色状态信息"
    },
    role_name: {
      field: "role_name",
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      comment: "角色名称"
    },
    desc: {
      field: "desc",
      type: DataTypes.STRING,
      comment: "角色备注"
    }
  },
    {
      tableName: "role",
      freezeTableName: true,
      timestamps: true,
      underscored: true
    });

  role.associate = function (models) {
    // 角色表包含管理员
    role.hasMany(models["admin"], {
      foreignKey: "by_role",
      constraints: false
    });
    // 角色表包含api_role表
    role.belongsToMany(models["api"], {
      through: {
        model: "api_role",
        unique: false
      },
      foreignKey: "role_id",
      otherKey: "api_id",
      constraints: false
    });
    // 角色表包含menu_role表
    role.belongsToMany(models["menu"], {
      through: {
        model: "menu_role",
        unique: false
      },
      foreignKey: "role_id",
      otherKey: "menu_id",
      constraints: false
    });
  };

  return role;
};

Api接口模型

module.exports = function (sequelize, DataTypes) {
  const api = sequelize.define("api",
    {
      id: {
        field: "id",                                    // 别名
        type: DataTypes.UUID,                           // 类型
        allowNull: true,
        unique: true,                                   // 是否是唯一值
        comment: "api接口字段ID",                        // 简介说明
        primaryKey: true,                               // 是否为主键
        defaultValue: DataTypes.UUIDV1
      },
      name: {
        field: "name",
        type: DataTypes.STRING,
        allowNull: false,
        comment: "api接口名称",
        validate: {
          notEmpty: true
        }
      },
      method: {
        field: "method",
        type: DataTypes.STRING,
        allowNull: false,
        comment: "api接口访问类型",
        validate: {
          notEmpty: true
        }
      },
      path: {
        field: "path",
        type: DataTypes.STRING,
        allowNull: false,
        comment: "api接口访问地址",
        validate: {
          notEmpty: true
        }
      },
      desc: {
        field: "desc",
        type: DataTypes.STRING,
        allowNull: true,
        comment: "api接口内容简介"
      },
      time: {
        field: "time",
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW
      },
      mark: {
        field: "mark",
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      query_field: {
        field: "query_field",
        type: DataTypes.TEXT,
        allowNull: true,
        comment: "api接口请求参数配置"
      },
      success_resource: {
        field: "success_resource",
        type: DataTypes.TEXT,
        allowNull: true,
        comment: "api接口请求成功参数配置"
      },
      error_resource: {
        field: "error_resource",
        type: DataTypes.TEXT,
        allowNull: true,
        comment: "api接口请求失败参数配置"
      }
    }, {
      timestamps: true,                // 创建createAt、updateAt字段
      underscored: true,               // 将字段名按照下划线命名
      freezeTableName: true,           // 不允许自动更换表明
      tableName: "api",                 // 表名
      /* hooks: {
        beforeBulkCreate: (api) => {
          console.log(api);
        }
      } */
    }
    // api表包含api_role表
  );
  api.associate = function (models) {
    // api表包含role
    api.belongsToMany(models["role"], {
      through: {
        model: "api_role",
        unique: false
      },
      foreignKey: "api_id",
      otherKey: "role_id",
      constraints: false
    });
  };
  return api;
}

用户角色表 role.png
Api接口表 apix.png 关联表 api.png
这边的需求是,删除用户角色表的数据,同时删除关联表的数据,不知道如何实现,请教一下cnode的各位,谢谢

4 回复

封贴问题已解决,贼坑。

Sequelize的文档中,没有明确的写出如何操作,通过Google,查询到相关问题,经过测试,目前找到解决方案。 明日整理一下,准备分享。

厉害了 楼主分享一下

@Ander456 定义模型关系时,需要定义onDelete和onUpdate,然后关键时,要注意一点时不要用自动生成表模型,要自己定义关联模型,对关联模型上的某个属性,比如我这边时role_id 和 api_id 在设置一次onDelete。 一定要自己写表模型,自动生成并不携带onDelete以及onUpdate。 一定要自己写。。。

回到顶部