Answer the question
In order to leave comments, you need to log in
How to select data from two tables using Sequelize?
I have been reading the documentation on Sequelize for several days now, but I just can’t make a selection from two tables that are associated with each other.
There are two tables:
users
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
+----+-------+
and
settings
+---------+-------+
| user_id | group|
+---------+-------+
| 1 |standard|
| 1 | Middle |
| 2 | Middle |
+---------+---------+
Tried to specify the relationship via
users.belongsTo(settings, {foreignKey: 'user_id', targetKey: 'id'})
and
users.belongsToMany( settings, { through: 'user_id' })
The sample was made through
Model.users.findAll({
attributes: ['name'],
where: {
id: {
[Op.in]: arUserID
}
},
include: [{
model: Model.settings,
attributes: ['group']
}]
})
Sequelize.define('settings',{ /* ... */ }, { tableName: 'settings'}
Answer the question
In order to leave comments, you need to log in
settings.belongsTo(users); // в сеттингс появляется поле userId
Model.settings.findAll({
include: [{
model: Model.users,
where: {
id: { [Op.in]: arUserID },
},
}],
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question