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
?
what about this?
Project.belongsTo(User)
http://docs.sequelizejs.com/class/lib/associations/belongs-to.js~BelongsTo.html
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.