Sequelize update with association(Sequelize 更新与关联)
问题描述
在 sequelize 中,可以像这样一次性创建一行及其所有关联:
In sequelize it's possible to create a row and all it's association in one go like this:
return Product.create({
title: 'Chair',
User: {
first_name: 'Mick',
last_name: 'Broadstone'
}
}, {
include: [ User ]
});
是否有等效的更新?我试过了
Is there a equivalent for update? I tried
model.user.update(req.body.user, {where: {id: req.user.user_id}, include: [model.profile]})
但它只是更新用户
这样做是为了创作作品
model.user.create(user, {transaction: t, include: [model.profile]})
推荐答案
首先你必须找到你想要更新的包含子模型的模型.然后您可以轻松获取子模型的参考以进行更新.我发布了一个示例供您参考.希望它会有所帮助.
First you have to find model including sub model which you want to update. then you can get reference of sub model to update easily. i am posting an example for your reference. hope it will help.
var updateProfile = { name: "name here" };
var filter = {
where: {
id: parseInt(req.body.id)
},
include: [
{ model: Profile }
]
};
Product.findOne(filter).then(function (product) {
if (product) {
return product.Profile.updateAttributes(updateProfile).then(function (result) {
return result;
});
} else {
throw new Error("no such product type id exist to update");
}
});
这篇关于Sequelize 更新与关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sequelize 更新与关联


基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01