module.exports = function (sequelize, DataTypes) { const Category = sequelize.define('Category', { id: { type: DataTypes.INTEGER, primaryKey: true }, name: { type: DataTypes.STRING, allowNull: false }, sort: { // Se cambia el nombre del campo de 'order' a 'sort' porque 'order' es una palabra reservada SQL y da problemas al generar las consultas SQL. field: 'order', // <- Chapuza!! El nombre del campo en MySQL es una palabra reservada en SQL. type: DataTypes.INTEGER, defaultValue: 0, allowNull: false, } }, { tableName: 'category', 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, { through: models.PostCategory, foreignKey: 'categoryId' }); }; return Category; };