module.exports = function (sequelize, DataTypes) { const Category = sequelize.define('Category', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true }, name: { type: DataTypes.STRING, allowNull: false }, sort: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false, } }, { tableName: 'categories', freezeTableName: true, timestamps: false }); /*Category.beforeCreate((category) => { //category.dataValues.id = Math.floor(Math.random() * (999 - 8)) + 8; })*/ Category.associate = function (models) { Category.Posts = Category.belongsToMany(models.Post, { as: 'posts', through: models.PostCategory, foreignKey: 'categoryId' }); }; return Category; };