code
tables and tag
tables are many-to-many relationships
code
Table model
"use strict";
module.exports = function(sequelize, DataTypes) {
var code = sequelize.define("code", {
markdown: DataTypes.TEXT,
title:DataTypes.STRING
}, {
timestamps: true,
paranoid: true,
classMethods: {
associate: function(models) {
code.belongsToMany(models["tag"], { through: "tagcode" })
}
}
});
return code;
};
tag
Table model
"use strict";
module.exports = function(sequelize, DataTypes) {
var tag = sequelize.define("tag", {
name: DataTypes.STRING,
}, {
timestamps: true,
paranoid: true,
classMethods: {
associate: function(models) {
tag.belongsToMany(models["code"], { through: "tagcode" })
}
}
});
return tag;
};
the above two model generate the tagcode
table.
tag
the table already has data.
dbModels.code.create({
title: req.body.title,
markdown: req.body.markdown,
tags: [
{ name: "Alpha"},
{ name: "Beta"}
]
},
{
include: {model:dbModels.tag }
}
).then(function (code) {
res.status(200).json(code)
})
the code above adds" Alpha""Beta" tag "every time you add code.
now wants to pass tagId (multiple) when adding code
records, and then add the corresponding association in the relational table tagcode, instead of adding data in the tag table.
could you tell me how to implement it?