Ahmed-Anas
Ahmed-Anas

Reputation: 5619

sequelize - how to do add additional fields to through

either the docs are old or I'm misunderstanding, or there's a bug. In any case:

const User = sequelize.define('user', {})
const Project = sequelize.define('project', {})
const UserProjects = sequelize.define('userProjects', {
    status: Sequelize.STRING
})

User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })

sequelize.sync().then(v => {

    var user, project;
    User.create({}).then(_user => {
        user = _user;

        Project.create({}).then(_project => {
            project = _project;
            // project.addUser(user, { through: { status: 'started' } })
            user.addProject(project, { through: { status: 'started' } });


        })
    })

})

The following should create an association with the status field populated, however it's null.

AM I missing something

Upvotes: 1

Views: 1025

Answers (1)

Yrysbek Tilekbekov
Yrysbek Tilekbekov

Reputation: 2775

Try to pass params without through

user.addProject(project, {status: 'started'});

I think there is some typo on current updated documentation. Here's previous docs v3 https://sequelize.readthedocs.io/en/v3/docs/associations/#belongs-to-many-associations

Upvotes: 1

Related Questions