37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
const sequelize = require('./config/db');
|
|
const { Room, Rental, Tenant, Contract } = require('./models');
|
|
|
|
// 清空数据的函数
|
|
async function clearData() {
|
|
try {
|
|
console.log('开始清空数据...');
|
|
|
|
// 按照依赖关系的顺序删除数据
|
|
// 先删除租房记录(依赖于房间、租客和合同)
|
|
await Rental.destroy({ where: {} });
|
|
console.log('租房数据已删除');
|
|
|
|
// 删除合同记录(依赖于房间和租客)
|
|
await Contract.destroy({ where: {} });
|
|
console.log('合同数据已删除');
|
|
|
|
// 删除房间记录(依赖于公寓)
|
|
await Room.destroy({ where: {} });
|
|
console.log('房间数据已删除');
|
|
|
|
// 删除租客记录(无依赖)
|
|
await Tenant.destroy({ where: {} });
|
|
console.log('租客数据已删除');
|
|
|
|
console.log('数据清空完成!');
|
|
} catch (error) {
|
|
console.error('清空数据时出错:', error);
|
|
} finally {
|
|
// 关闭数据库连接
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
// 执行清空操作
|
|
clearData();
|