Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

sequlize 连接表 设置器传额外的对象 没有效果

题目描述

定义了两个表 user表以及team表 因为是多对多的对应关系 所以定义了一个关联表.

User.belongsToMany(Team, {
    through: TeamMember, 
    as: 'TeamMember',
    foreignKey: 'user_id',
    constraints: false
})
Team.belongsToMany(User, {
    through: TeamMember, 
    as: 'TeamMember',
    foreignKey: 'team_id',
    constraints: false
})

但希望在中间表中再添加一个字段用来表示用户权限

Promise.all([
    Team.create({
        ......
    }),
    User.findOne({
        where: {
            id: ......
        }
    })
]).then(results => {
    var team = results[0];
    var user = results[1];
    team.addTeamMember(user, { role: 0 })
})

这行代码但并没有在关联表设置role字段,请问各位大佬是哪里出现问题了呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

自己回答一下吧 看的是旧的开发文档 最新版本

team.addTeamMember(user, {
                through: {
                    role: 0
                }
             })

即可


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...