Answer the question
In order to leave comments, you need to log in
How to execute group in Sequelize?
Hello, there are 3 tables:
Users:
const users = sequelize.define('users', {
login: {
type: Sequelize.STRING,
allowNull: true,
},
email: {
type: Sequelize.STRING,
allowNull: true
},
password: {
type: Sequelize.STRING,
allowNull: false
}
})
const messengers = sequelize.define('messengers', {
user_id: {
type: Sequelize.INTEGER,
allowNull: false,
foreignKey: true
},
messenger_id: {
type: Sequelize.STRING,
allowNull: true
},
login: {
type: Sequelize.STRING,
allowNull: true
}
})
const messengerMessages = sequelize.define('messenger_messages', {
messenger_id: {
type: Sequelize.INTEGER,
allowNull: false,
foreignKey: true
},
text: {
type: Sequelize.TEXT,
allowNull: true
}
})
users.hasMany(messengers, { foreignKey: 'user_id', as: 'userMessenger' })
messengers.belongsTo(users, { foreignKey: 'user_id', as: 'userMessenger' })
messengers.hasMany(messengerMessages, { foreignKey: 'messenger_id', as: 'messengerMessages' })
messengerMessages.belongsTo(messengers, { foreignKey: 'messenger_id', as: 'messengerMessages' })
await models.messengerMessages.findAll({
include: [{
model: models.messengers,
as: 'messengerMessages',
include: [{
model: models.users,
as: 'userMessenger',
}]
}],
group: ['messengerMessages->userMessenger.id']
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question