96 lines
3.3 KiB
JavaScript
96 lines
3.3 KiB
JavaScript
|
|
// 完整模型测试脚本
|
||
|
|
const sequelize = require('./config/database');
|
||
|
|
|
||
|
|
// 确保先导入所有依赖模型
|
||
|
|
const Department = require('./models/Department');
|
||
|
|
const Position = require('./models/Position');
|
||
|
|
const AdminStaff = require('./models/AdminStaff');
|
||
|
|
|
||
|
|
async function fullModelTest() {
|
||
|
|
try {
|
||
|
|
console.log('开始完整模型测试...');
|
||
|
|
|
||
|
|
// 测试数据库连接
|
||
|
|
await sequelize.authenticate();
|
||
|
|
console.log('✅ 数据库连接成功');
|
||
|
|
|
||
|
|
// 检查模型是否被正确导入
|
||
|
|
console.log('\n检查模型导入状态:');
|
||
|
|
console.log('Department 模型:', Department ? '✅ 已导入' : '❌ 未导入');
|
||
|
|
console.log('Position 模型:', Position ? '✅ 已导入' : '❌ 未导入');
|
||
|
|
console.log('AdminStaff 模型:', AdminStaff ? '✅ 已导入' : '❌ 未导入');
|
||
|
|
|
||
|
|
// 测试基本的查询功能
|
||
|
|
console.log('\n测试基本查询功能:');
|
||
|
|
|
||
|
|
// 查询部门表
|
||
|
|
console.log('\n查询部门表:');
|
||
|
|
try {
|
||
|
|
const departments = await Department.findAll({ limit: 5 });
|
||
|
|
console.log(`找到 ${departments.length} 个部门`);
|
||
|
|
if (departments.length > 0) {
|
||
|
|
console.log('示例部门:', departments[0].dataValues);
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('部门查询失败:', error.message);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 查询岗位表
|
||
|
|
console.log('\n查询岗位表:');
|
||
|
|
try {
|
||
|
|
const positions = await Position.findAll({ limit: 5 });
|
||
|
|
console.log(`找到 ${positions.length} 个岗位`);
|
||
|
|
if (positions.length > 0) {
|
||
|
|
console.log('示例岗位:', positions[0].dataValues);
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('岗位查询失败:', error.message);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 查询行政人员表
|
||
|
|
console.log('\n查询行政人员表:');
|
||
|
|
try {
|
||
|
|
const adminStaffs = await AdminStaff.findAll({ limit: 5 });
|
||
|
|
console.log(`找到 ${adminStaffs.length} 个行政人员`);
|
||
|
|
if (adminStaffs.length > 0) {
|
||
|
|
console.log('示例行政人员:', adminStaffs[0].dataValues);
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('行政人员查询失败:', error.message);
|
||
|
|
console.error('错误详情:', error);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试关联查询
|
||
|
|
console.log('\n测试关联查询:');
|
||
|
|
try {
|
||
|
|
const adminStaffWithRelations = await AdminStaff.findAll({
|
||
|
|
include: [
|
||
|
|
{ model: Department, as: 'department' },
|
||
|
|
{ model: Position, as: 'position' }
|
||
|
|
],
|
||
|
|
limit: 1
|
||
|
|
});
|
||
|
|
console.log('关联查询结果数量:', adminStaffWithRelations.length);
|
||
|
|
if (adminStaffWithRelations.length > 0) {
|
||
|
|
console.log('关联查询成功,已获取到关联数据');
|
||
|
|
// 只打印部分数据以避免输出过多
|
||
|
|
const staff = adminStaffWithRelations[0];
|
||
|
|
console.log('姓名:', staff.name);
|
||
|
|
console.log('部门:', staff.department?.name || '未知');
|
||
|
|
console.log('岗位:', staff.position?.name || '未知');
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('关联查询失败:', error.message);
|
||
|
|
console.error('错误详情:', error);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('\n✅ 完整模型测试完成');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ 完整模型测试失败:', error.message);
|
||
|
|
console.error('错误详情:', error);
|
||
|
|
} finally {
|
||
|
|
await sequelize.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fullModelTest();
|