Please tell me how to add and query data in sequelize many-to-many relationship.

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?

Mar.16,2021

Thank you for inviting me. How to deploy sequelize


?


 dbModels.code.create({
        title: req.body.title,
        codeCategoryId: req.body.type,
        limit: 1,
        markdown: req.body.markdown,
    }).then(function (code) {
        dbModels.tag.findAll({where: {id: req.body.tags}}).then(function (tags) {
            code.setTags(tags)
            return res.status(200).json({
                message:''
            })
        })
    })
  • How sequelize-cli runs a single seeder

    in sequelize-cli, sequelize db:seed:all executes all seeder in the seeders folder. if you only want to execute a certain seeder, such as the 20180906061439-user2.js file, how do you write it on the command line? $ sequelize --help Sequelize CLI...

    Aug.27,2021
  • How does sequelize query the data within seven days?

    In mysql, the time type is datetime , orm is sequelize , how to get the data within 7 days and 30 days in the table, how to write the query conditions (is it where date_col > 7 days ago datetime), and whether sequelize provides a more convenient way...

    Jun.29,2022
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e44094-44a6f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e44094-44a6f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?