【问题标题】:Sequelize update with association not updating table for the associated modelSequelize update with association 不更新关联模型的表
【发布时间】:2018-09-11 19:14:03
【问题描述】:

我在下面有两个表格: 1.Poll 2.Option

投票模型如下:

 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 路由

我想更新投票。所有其他路线都运行良好。我只是坚持更新模型。

/PUT api/poll/:id 的控制器

/PUT api/poll/:id 的服务

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

我也试过了! Sequelize update with association 我什至做了与 sequelize docs 相同的操作。但我无法实现我想要的。我已经坚持了一段时间。任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 我想你可能需要留意你的ON UPDATE

标签: javascript node.js orm associations sequelize.js


【解决方案1】:

对于更新,您可以尝试 SequelizeModel.upsert() 函数。

【讨论】:

  • 未更新关联值
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
相关资源
最近更新 更多