const { DataTypes } = require('sequelize'); const { sequelize } = require('../config/database'); const Menu = sequelize.define('Menu', { id: { type: DataTypes.INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '메뉴 고유 식별자' }, parentId: { type: DataTypes.INTEGER.UNSIGNED, allowNull: true, references: { model: 'Menus', // 실제 테이블명 key: 'id' }, comment: '상위 메뉴 ID' }, title: { type: DataTypes.STRING, allowNull: false, comment: '메뉴명' }, icon: { type: DataTypes.STRING, allowNull: true, comment: '아이콘' }, to: { type: DataTypes.STRING, allowNull: false, comment: '라우팅 경로' }, featureCode: { type: DataTypes.STRING, allowNull: true, references: { model: 'Features', key: 'code' }, comment: '연결된 기능 코드' }, role: { type: DataTypes.STRING, allowNull: false, defaultValue: 'user', comment: '권한 역할' }, order: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: '정렬 순서' }, visible: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true, comment: '표시 여부' }, createdAt: { type: DataTypes.DATE, allowNull: false, comment: '생성일' }, updatedAt: { type: DataTypes.DATE, allowNull: false, comment: '수정일' } }, { tableName: 'Menus', comment: '서비스 내 메뉴 구조 및 정보를 저장하는 테이블', timestamps: true }); Menu.associate = (models) => { Menu.belongsTo(models.Feature, { foreignKey: 'featureCode', targetKey: 'code', }); Menu.belongsTo(models.Menu, { as: 'parent', foreignKey: 'parentId', }); Menu.hasMany(models.Menu, { as: 'children', foreignKey: 'parentId', }); }; // 자기참조 관계를 associate에서 명확히 선언 Menu.associate = (models) => { Menu.hasMany(models.Menu, { foreignKey: 'parentId', as: 'children' }); Menu.belongsTo(models.Menu, { foreignKey: 'parentId', as: 'parent' }); }; module.exports = Menu;