'use strict'; module.exports = app => { const { BIGINT, INTEGER, DATE } = app.Sequelize; const RolePermission = app.model.define('role_permission', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '关联ID', }, role_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '角色ID', }, permission_id: { type: INTEGER, allowNull: false, comment: '权限ID', }, created_at: { type: DATE, allowNull: false, comment: '创建时间', }, }, { tableName: 'wms_role_permission', freezeTableName: true, underscored: true, timestamps: true, paranoid: false, // 明确禁用软删除 createdAt: 'created_at', updatedAt: false, comment: '角色权限关联表', indexes: [ { name: 'uk_role_permission', fields: [ 'role_id', 'permission_id' ], unique: true, }, { name: 'idx_role_id', fields: [ 'role_id' ], }, { name: 'idx_permission_id', fields: [ 'permission_id' ], }, ], }); // 关联关系 RolePermission.associate = function(models) { const { Role, Permission } = app.model; // 关联角色 RolePermission.belongsTo(Role, { foreignKey: 'role_id', as: 'role', }); // 关联权限 RolePermission.belongsTo(Permission, { foreignKey: 'permission_id', as: 'permission', }); }; // 实例方法:检查关联是否有效 RolePermission.prototype.isValid = function() { // 这里可以添加更多的有效性检查 return this.role_id && this.permission_id; }; return RolePermission; };