2025-09-17 18:04:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 模型索引文件
|
|
|
|
|
|
* @file index.js
|
|
|
|
|
|
* @description 导出所有模型并建立关联关系
|
|
|
|
|
|
*/
|
|
|
|
|
|
const { sequelize } = require('../config/database');
|
|
|
|
|
|
|
|
|
|
|
|
// 导入所有模型
|
|
|
|
|
|
const User = require('./User');
|
|
|
|
|
|
const Role = require('./Role');
|
|
|
|
|
|
const Account = require('./Account');
|
|
|
|
|
|
const Transaction = require('./Transaction');
|
2025-09-22 17:56:30 +08:00
|
|
|
|
const LoanProduct = require('./LoanProduct');
|
|
|
|
|
|
const Employee = require('./Employee');
|
|
|
|
|
|
const Department = require('./Department');
|
|
|
|
|
|
const Position = require('./Position');
|
|
|
|
|
|
const Report = require('./Report');
|
2025-09-17 18:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
// 定义模型关联关系
|
|
|
|
|
|
|
|
|
|
|
|
// 用户与角色关联
|
|
|
|
|
|
User.belongsTo(Role, {
|
|
|
|
|
|
foreignKey: 'role_id',
|
|
|
|
|
|
as: 'role',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Role.hasMany(User, {
|
|
|
|
|
|
foreignKey: 'role_id',
|
|
|
|
|
|
as: 'users'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 用户与账户关联
|
|
|
|
|
|
User.hasMany(Account, {
|
|
|
|
|
|
foreignKey: 'user_id',
|
|
|
|
|
|
as: 'accounts'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Account.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'user_id',
|
|
|
|
|
|
as: 'user'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 账户与交易记录关联
|
|
|
|
|
|
Account.hasMany(Transaction, {
|
|
|
|
|
|
foreignKey: 'account_id',
|
|
|
|
|
|
as: 'transactions'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Transaction.belongsTo(Account, {
|
|
|
|
|
|
foreignKey: 'account_id',
|
|
|
|
|
|
as: 'account'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 交易记录与用户关联(通过账户)
|
|
|
|
|
|
// 移除不合理的Transaction->User through Account的belongsTo定义,避免错误外键映射
|
|
|
|
|
|
|
2025-09-22 17:56:30 +08:00
|
|
|
|
// 员工与部门关联
|
|
|
|
|
|
Employee.belongsTo(Department, {
|
|
|
|
|
|
foreignKey: 'department_id',
|
|
|
|
|
|
as: 'department',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Department.hasMany(Employee, {
|
|
|
|
|
|
foreignKey: 'department_id',
|
|
|
|
|
|
as: 'employees'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 员工与职位关联
|
|
|
|
|
|
Employee.belongsTo(Position, {
|
|
|
|
|
|
foreignKey: 'position_id',
|
|
|
|
|
|
as: 'position',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Position.hasMany(Employee, {
|
|
|
|
|
|
foreignKey: 'position_id',
|
|
|
|
|
|
as: 'employees'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 报表与用户关联
|
|
|
|
|
|
Report.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(Report, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'reports'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-17 18:04:28 +08:00
|
|
|
|
// 导出所有模型和数据库实例
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
sequelize,
|
|
|
|
|
|
User,
|
|
|
|
|
|
Role,
|
|
|
|
|
|
Account,
|
2025-09-22 17:56:30 +08:00
|
|
|
|
Transaction,
|
|
|
|
|
|
LoanProduct,
|
|
|
|
|
|
Employee,
|
|
|
|
|
|
Department,
|
|
|
|
|
|
Position,
|
|
|
|
|
|
Report
|
2025-09-17 18:04:28 +08:00
|
|
|
|
};
|