[英]Sequelize update or create included association
Having Sequelize hasOne
associations: 具有Sequelize
hasOne
关联:
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? 我们如何添加新
Project
并将其连接到User
实例?
Firstly, we need to find the User
: 首先,我们需要找到
User
:
const user = await models.users.findOne({
where: { id: 1 },
include: models.projects
});
What do we do next? 接下来我们要做什么? What if
user.project
is null
? 如果
user.project
为null
怎么办?
what about this? 那这个呢?
Project.belongsTo(User)
http://docs.sequelizejs.com/class/lib/associations/belongs-to.js~BelongsTo.html http://docs.sequelizejs.com/class/lib/associations/belongs-to.js~BelongsTo.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.