import db from '../src/database/models/index.js'; async function checkAreaManager() { try { console.log('Connecting to database...'); await db.sequelize.authenticate(); console.log('Database connected.'); // Fetch all areas const areas = await db.Area.findAll({ include: [ { model: db.User, as: 'manager', attributes: ['id', 'fullName'] } ] }); console.log(`Found ${areas.length} areas.`); if (areas.length > 0) { areas.forEach((area: any) => { console.log(`Area: ${area.areaName} (${area.id})`); console.log(` - Manager ID (Field): ${area.managerId}`); console.log(` - Manager (Association): ${area.manager ? area.manager.fullName : 'None'}`); console.log('-----------------------------------'); }); // Pick the first area and try to update it manually if managerId is null const targetArea = areas[0]; // Find a user to assign (any user) const user = await db.User.findOne(); if (user) { console.log(`Attempting to assign User ${user.fullName} (${user.id}) to Area ${targetArea.areaName}...`); targetArea.managerId = user.id; await targetArea.save(); console.log('Update saved. Re-fetching to verify...'); const updatedArea = await db.Area.findByPk(targetArea.id); console.log(`Re-fetched Area Manager ID: ${updatedArea?.managerId}`); if (updatedArea?.managerId === user.id) { console.log('SUCCESS: Manager ID persisted correctly.'); } else { console.error('FAILURE: Manager ID did not persist.'); } } else { console.log('No users found to test assignment.'); } } else { console.log('No areas found.'); } } catch (error) { console.error('Error:', error); } finally { await db.sequelize.close(); } } checkAreaManager();