简体   繁体   English

使用Sequelize更新关联表数据的工作示例?

[英]Working example for updating associative table data with Sequelize?

This is my model: 这是我的模型:

 var Vote = sequelize.define('vote', { title: { type: Sequelize.STRING, allowNull: false }, description: { type: Sequelize.TEXT } }); var Choice = sequelize.define('choice', { choiceTitle: { type: Sequelize.STRING, allowNull: false }, count: { type: Sequelize.INTEGER, length: 6 } }); Choice.belongsTo(Vote); Vote.hasMany(Choice, {onUpdate: 'cascade'}); 

Below is the code to update the 'votes' table including associated 'choices' table if possible using 'vote' argument of 'store()' which is already an object of a Vote with updated values including 'choices' from the app. 以下是使用“ store()”的“ vote”参数更新“ votes”表(包括关联的“ choices”表)的代码,该参数已经是Vote的对象,具有更新的值,包括来自应用程序的“ choices”。

 store(vote, callback) { return Vote.findById(vote.id, { include: [ { model: Choice, as: 'choices' } ] }).then(function(resVote) { resVote.choices[0].updateAttributes({count: 120}) // just to test if at least it gets written to db .then(function(res) { callback(null, res); }).catch((err) => callback(err, null)); }); }, 

FYI: In the code I just set one attribute of a specific choice to 120, just to see if it gets written down in the postgres-db, and, hooray, that works (thanks to Jan Aagaard's reply on Updating attributes in associated models using Sequelize to at least have an update on associated table). 仅供参考:在代码中,我只是将特定选择的一个属性设置为120,只是为了查看它是否在postgres-db中被写下,并且万岁,这是可行的(感谢Jan Aagaard对使用以下方法更新关联模型中的属性的回复: Sequelize至少有关联表的更新)。

I did not really find a solution in Sequelize's docs (at least they have some). 我在Sequelize的文档中没有真正找到解决方案(至少他们有一些解决方案)。 When I try to 'save()', 'update()' or 'upsert()' vote directly it just creates a completely new row with empty fields if null is allowed, or simply fails if null is disallowed. 当我尝试直接进行'save()','update()'或'upsert()'投票时,如果允许使用null,则只会创建一个带有空字段的全新行,如果不允许使用null,则只会失败。

What is the recommended way to update all (changed) fields of a table and if possible also an associated table in Sequelize? 建议使用什么方法来更新表的所有(更改的)字段,如果可能的话,还可以更新Sequelize中的关联表? Sorry if this question is a bit basic, but I'm quite stuck right now and did not find a clear answer for this task. 抱歉,这个问题有点基本,但是我现在很困惑,没有找到明确的答案。

You can pass an include obj to creates (see under "Creating with associations" on http://sequelize.readthedocs.io/en/v3/docs/associations/ ) but it doesn't look like you can do so with updates. 您可以传递一个include obj来进行创建(请参见http://sequelize.readthedocs.io/en/v3/docs/associations/上的“使用关联进行创建”),但是您似乎无法通过更新来完成。

I think what you want to do is fetch the related record(s) using the relationship accessors Sequelize gives you and update those instances: 我认为您想要做的是使用Sequelize给您的关系访问器获取相关记录并更新这些实例:

 Vote.findById(vote.id)
  .then(function(vote) {
    return vote.getChoices()
  })
  .then(function(choices) {
    return choices[0].update({ count: 120 })
  })
  .then(function(choice) {
    callback(null, choice);
  })
  .catch(function(err) {
    callback(err);
  });

  (I realise this means you lose the join and end up with two separate queries, but I can't think another way of doing this just now)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM