简体   繁体   中英

Sequelize update or create included association

Having Sequelize hasOne associations:

const User = sequelize.define('user', {/* ... */})
const Project = sequelize.define('project', {/* ... */})

// One-way association
User.hasOne(Project)

and user instance already exists . How do we add a new Project and connect it to the User instance?

Firstly, we need to find the User :

const user = await models.users.findOne({
    where: { id: 1 },
    include: models.projects
});

What do we do next? What if user.project is null ?

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