2
rated 0 times
[
4]
[
2]
/ answers: 1 / hits: 18158
/ 8 Years ago, tue, august 2, 2016, 12:00:00
Is it possible to include a model for an attribute of a many-to-many relation via through
?
Let's say I'm having the following Models:
User = sequelize.define('user', {/* attributes */});
Question = sequelize.define('question', {/* attributes */});
Answer = sequelize.define('answer', {/* attributes */});
/* Where */
Question.hasMany(Answer);
Answer.belongsTo(Question);
I'd like to save for every user which answer he gave to a question. So I added the following relation:
UserQuestionAnswerRel = sequelize.define('usersAnwers', {
user: {/**/},
question: {/**/},
answer: {/**/}
});
User.hasMany(Question, { through: UserQuestionAnswerRel });
Now I'd like to query a user receiving all questions he answered and the answer he gave. But how does that work? Unfortunately this doesn't work:
User.findAll({
include: {
model: Question,
through: { include: model.Answer }
}
}).then /* */
Please note: All this Q&A stuff is just an example. It won't be possible for me to not use a through
relation.
More From » sequelize.js