12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- // models/Category.js
- module.exports = (sequelize, DataTypes) => {
- const Category = sequelize.define('Category', {
- id: {
- type: DataTypes.INTEGER.UNSIGNED,
- primaryKey: true,
- autoIncrement: true
- },
- name: {
- type: DataTypes.STRING,
- allowNull: false,
- comment: '品类名称'
- },
- level: {
- type: DataTypes.INTEGER,
- defaultValue: 1,
- comment: '层级深度'
- },
- description: {
- type: DataTypes.TEXT,
- comment: '品类描述'
- },
- parentId: {
- type: DataTypes.INTEGER.UNSIGNED,
- allowNull: true,
- comment: '父级品类ID'
- },
- order: {
- type: DataTypes.INTEGER.UNSIGNED,
- defaultValue: 0,
- comment: '排序顺序'
- },
- isActive: {
- type: DataTypes.BOOLEAN,
- defaultValue: true,
- comment: '是否启用'
- }
- }, {
- tableName: 'categories',
- timestamps: true,
- indexes: [
- {
- fields: ['parentId']
- },
- {
- fields: ['name']
- }
- ]
- });
- // 添加associate方法
- Category.associate = function(models) {
- // 自关联关系
- Category.hasMany(Category, {
- as: 'children',
- foreignKey: 'parentId',
- onDelete: 'CASCADE'
- });
- Category.belongsTo(Category, {
- as: 'parent',
- foreignKey: 'parentId'
- });
- };
- return Category;
- };
|