I'm trying to define a M:N relationship between models Survey and Question. The name of the relationship is SurveyHasQuestions which also has a definition (see further down how it's used in the associations definitions):
var SurveyHasQuestions = sequelize.define('survey_has_questions', {
shq_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
tableName: 'survey_has_questions'
});
Tables for Survey and Question are generated correctly in the DB (which btw is Postgres):
survey_id: integer (pkey)
survey_url: string
date: timestamp
and
question_id: integer (pkey)
question_text: string
Now, the following associations:
Survey.hasMany(SurveyQuestion, {through: SurveyHasQuestions, foreignKey: 'survey_id'});
SurveyQuestion.hasMany(Survey, {through: SurveyHasQuestions, foreignKey: 'question_id'});
SurveyHasQuestions.belongsTo(Survey, {foreignKey: 'survey_id'});
SurveyHasQuestions.belongsTo(SurveyQuestion, {foreignKey: 'question_id'});
work correctly i.e. they generate a survey_has_questions table for the M:N relationship with the desired structure:
shq_id: integer (pkey)
question_id: integer (fkey references survey_question.question_id)
survey_id: integer (fkey references survey.survey_id)
but sequelize complains with the warning: Using 2 x hasMany to represent N:M relations has been deprecated. Please use belongsToMany instead
So in an effort to do things correctly, I've tried using only belongsToMany(). But these associations:
SurveyQuestion.belongsToMany(Survey, {
through: SurveyHasQuestions,
foreignKey: 'question_id'
});
Survey.belongsToMany(SurveyQuestion, {
through: SurveyHasQuestions,
foreignKey: 'survey_id'
});
generate for survey_has_questions the incorrect table:
shq_id: integer (pkey)
question_id: integer (fkey references survey_question.question_id)
survey_survey_id: integer (fkey references survey.survey_id) <---??? UNWANTED
survey_id: integer (fkey references survey.survey_id)
The problem is the extra column survey_survey_id which serves absolutely nothing as it's a duplicate of the other column survey_id.
The interesting part is that if I reverse the order of the .belongsToMany() statements, I get an extra field survey_question_question_id in place of the survey_survey_id.
Now I know that I can fix this situation if in the definition of SurveyHasQuestions I remove my own primary key shq_id and let the combination of the fkeys serve as the pkey. But even though technically the serial pkey may not offer anything in the relationship (it might even be an overhead), still as far as I know it's not illegal to define one.
Anyone else come across this kind of behavior? Is there a way to work around it i.e. define associations using only belongsToMany() and still get the correct table structure for survey_has_questions?
This was a bug: https://github.com/sequelize/sequelize/issues/2991
It has since been fixed in the latest release, which is in v2.0.3: https://github.com/v12/sequelize/commit/73f875a935db911621ece8c2d86685c22c79a90e
Get v2.0.3 here: https://github.com/sequelize/sequelize/releases/tag/v2.0.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With