Friday 26 June 2020

Sequelize ORM

const User = sequelize.define('User',{name: DataTypes.STRING(99),email: DataTypes.STRING(99),password: DataTypes.STRING,active: DataTypes.BOOLEAN,},{tableName: 'users',hooks: {beforeSave: async (user) => {if (user.password) {user.password = await bcrypt.hash(user.password, 8)}},},})User.associate = function (models) {console.log(models)User.belongsToMany(models.Clinica, {foreignKey: 'user_id',as: 'clinicas',through: 'users_clinicas',})module.exports = UserThis code always return: Association with alias "clinicas" does not exist on UserWhen I run:User.findAll({include: [{association: 'clinicas',where: {id: 1,},attributes: [],},],attributes: ['id', 'name', 'email', 'profile_id'],logging: console.log,})

Submitted June 26, 2020 at 11:29PM by gofgohorse

No comments:

Post a Comment