javascript - Sequelize update with association 不更新关联模型的表

标签 javascript node.js orm associations sequelize.js

下面有两个表: 1. 投票 2. 选项

Poll模型如下:

 const Sequelize = require('sequelize');
 let dbService = require('../services/dbService.js');

 let Option = require('./options');

 let Poll = dbService.define('Poll', {
 poll: {
    type: Sequelize.STRING,
    allowNull: false
 },
 isAnonymous: {
    type: Sequelize.STRING,
    allowNull: false
 },
 startDate: {
    type: Sequelize.STRING,
 },
 endDate: {
    type: Sequelize.STRING,
 },
 active: {
    type: Sequelize.BOOLEAN,

 }
 });

 Poll.hasMany(Option);

 module.exports = Poll;

Option模型如下:

const Sequelize = require('sequelize');
let dbService = require('../services/dbService.js');

let Option = dbService.define("option", {
    name: Sequelize.STRING
});

module.exports = Option;

这是我的 api/poll/:id 的/GET 路由

This is my /GET route for api/poll/:id

我想更新投票。所有其他路线都工作正常。我只是坚持更新模型。

/PUT api/poll/:id 的 Controller enter image description here

/PUT api/poll/:id 的服务 enter image description here

这只会更新 Poll 表,不会更新 Option 表的列。如何通过这样的一对多关联实现更新?

我也试过了! Sequelize update with association 我什至做了与 Sequelize 文档相同的事情。但是我无法实现我想要的。我已经坚持了一段时间。任何有点帮助将不胜感激。 谢谢!

最佳答案

对于更新,您可以尝试使用 Sequelize Model.upsert() 函数。

关于javascript - Sequelize update with association 不更新关联模型的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49613497/

相关文章:

javascript - Express session 表现不同 : How can I prevent my session from being destroyed after a page refresh?

javascript - Node.js http.request options 主机名参数的正确格式是什么?

node.js - socket.io 在带有 node.js 的 webkit 移动版中不起作用

node.js - MongoDB 的奇怪日期行为

perl - DBIx::Class 如何处理 bool 值?

javascript - 另一个 GMap 问题

javascript - 如何在 kendo ui 中增加选择标签的边框宽度

javascript - 获取文本区域和文本字段数据 - jquery

java - Hibernate 从外部事务类中的 getCurrentPrice 返回 null

node.js - 如何在数据库中已定义表时使用 ORM?