Sequelize で belongsToManyで関連付けられたモデルのupdateの仕方が分かりません。
js
1const content = sequelize.define( 2 'content', 3 { 4 id: { 5 type: DataTypes.INTEGER.UNSIGNED, 6 field: 'id', 7 autoIncrement: true, 8 primaryKey: true, 9 allowNull: false 10 }, 11 }, 12); 13 14const tag = sequelize.defifne( 15 'tag', 16 { 17 id: { 18 type: DataTypes.INTEGER.UNSIGNED, 19 field: 'id', 20 autoIncrement: true, 21 primaryKey: true, 22 allowNull: false 23 }, 24 }, 25); 26 27content.belongsToMany(tag, { 28 through: 'contents_tags', 29 as: 'tags', 30 foreignKey: 'content_id', 31 otherKey: 'tag_id' 32}); 33 34 35tag.belongsToMany(content, { 36 through: 'contents_tags', 37 as: 'contents', 38 foreignKey: 'tag_id', 39 otherKey: 'content_id' 40}); 41
このように関連付けられたモデルがあった時、content
を、tag
と同期させながら update をしたくて、下記のようなコードを書いたのですが、うまく行きませんでした。
js
1content.update( 2 { 3 tags: [ 4 { 5 name: 'タグ' 6 } 7 ] 8 }, 9 { 10 where: { 11 id: ID 12 }, 13 include: [ 14 { 15 model: tag, 16 as: 'tags' 17 } 18 ] 19 } 20);
これをうまくできる方法を教えてください!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。