74 lines
2.4 KiB
JavaScript
74 lines
2.4 KiB
JavaScript
// 测试模型文件
|
|
const sequelize = require('./config/database');
|
|
const AdminStaff = require('./models/AdminStaff');
|
|
const Department = require('./models/Department');
|
|
const Position = require('./models/Position');
|
|
|
|
async function testModels() {
|
|
try {
|
|
console.log('开始测试模型...');
|
|
|
|
// 测试数据库连接
|
|
await sequelize.authenticate();
|
|
console.log('✅ 数据库连接成功');
|
|
|
|
// 测试模型关系
|
|
console.log('\n测试模型关系:');
|
|
console.log('AdminStaff关联Department:', AdminStaff.associations.department ? '✅' : '❌');
|
|
console.log('AdminStaff关联Position:', AdminStaff.associations.position ? '✅' : '❌');
|
|
console.log('Department关联Position:', Department.associations.positions ? '✅' : '❌');
|
|
console.log('Position关联Department:', Position.associations.department ? '✅' : '❌');
|
|
|
|
// 尝试获取行政人员数据
|
|
console.log('\n尝试获取行政人员数据:');
|
|
const adminStaffs = await AdminStaff.findAll({
|
|
include: [
|
|
{ model: Department, as: 'department' },
|
|
{ model: Position, as: 'position' }
|
|
],
|
|
limit: 5
|
|
});
|
|
|
|
if (adminStaffs.length > 0) {
|
|
console.log(`✅ 成功获取${adminStaffs.length}条行政人员数据`);
|
|
console.log('示例数据:', adminStaffs[0].dataValues);
|
|
} else {
|
|
console.log('⚠️ 未找到行政人员数据');
|
|
}
|
|
|
|
// 尝试获取部门数据
|
|
console.log('\n尝试获取部门数据:');
|
|
const departments = await Department.findAll({
|
|
limit: 5
|
|
});
|
|
|
|
if (departments.length > 0) {
|
|
console.log(`✅ 成功获取${departments.length}条部门数据`);
|
|
console.log('示例数据:', departments[0].dataValues);
|
|
} else {
|
|
console.log('⚠️ 未找到部门数据');
|
|
}
|
|
|
|
// 尝试获取岗位数据
|
|
console.log('\n尝试获取岗位数据:');
|
|
const positions = await Position.findAll({
|
|
include: [{ model: Department, as: 'department' }],
|
|
limit: 5
|
|
});
|
|
|
|
if (positions.length > 0) {
|
|
console.log(`✅ 成功获取${positions.length}条岗位数据`);
|
|
console.log('示例数据:', positions[0].dataValues);
|
|
} else {
|
|
console.log('⚠️ 未找到岗位数据');
|
|
}
|
|
|
|
console.log('\n✅ 模型测试完成');
|
|
} catch (error) {
|
|
console.error('❌ 模型测试失败:', error);
|
|
} finally {
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
testModels(); |