简体   繁体   中英

Sequelize many-to-many relationship table gets unneeded extra column

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 ie 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 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 ie define associations using only belongsToMany() and still get the correct table structure for survey_has_questions ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM