J
J
jenya77712019-07-22 19:35:59
Node.js
jenya7771, 2019-07-22 19:35:59

How to execute a query through Sequelize?

Hello, there are 2 related tables users and tasks.
How can I compose a query so that I would get one record of a user who has < 10 taks?

users.hasMany(tasks, { foreignKey: 'id_user', as: 'userTasks' })
tasks.belongsTo(users, { foreignKey: 'id_user', as: 'userTasks' })

users.findOne({
    include: [{
        model: models.tasks,
        as: 'userTasks'
    }]
}).then(data => {

    console.log(data)
}).catch(error => {

    console.error(error)
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question