简体   繁体   中英

Double association in Sequelize model

I have these associations

// Tasks.js
sequelize.models.tasks.belongsTo(sequelize.models.users, {
    foreignKey: {
        type: DataTypes.INTEGER,
        field: 'ASSIGNEE',
        allowNull: true
    },
    as: 'assignee'
});

sequelize.models.tasks.belongsTo(sequelize.models.users, {
    foreignKey: {
        type: DataTypes.INTEGER,
        field: 'REPORTER',
        allowNull: true
    },
    as: 'reporter'
});

// Users.js
sequelize.models.users.hasMany(sequelize.models.tasks, { as: 'assignee' });
sequelize.models.users.hasMany(sequelize.models.tasks, { as: 'reporter' });

These association create two FK: ASSIGNEE and REPORTER. But it also creates another one: userId. I know this "userId" FK is default, but I don't want it. I want just these two that I described in my associations.

How can I ask sequelize to ignore the default and create just what I was expecting?

Change in users association

sequelize.models.users.hasMany(sequelize.models.tasks, { as: 'assignee' ,foreignKey :'ASSIGNEE'});
sequelize.models.users.hasMany(sequelize.models.tasks, { as: 'reporter' ,foreignKey :'REPORTER'});

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