序列化belongsToMany将记录插入联接表

时间:2019-01-15 19:14:42

标签: javascript node.js sequelize.js

我有一个简单的数据库架构,其中有一个<div data-a data-b data-c="" class="big red"></div> data-a = type(data-a) = <type 'str'> len(data-a) = 0 data-c = type(data-c) = <type 'str'> len(data-c) = 0 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK 表,一个org表和一个tag表,这是我的联接表。

orgTags模型

org

'use strict'; module.exports = (sequelize, DataTypes) => { var organization = sequelize.define('organization', { id: { allowNull: false, primaryKey: true, type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, }, name: { allowNull: false, type: DataTypes.STRING, } }, {}); organization.associate = function (models) { // associations can be defined here organization.belongsToMany(models.tag, { through: 'orgTags' }) }; return organization; }; 模型

tag

'use strict'; module.exports = (sequelize, DataTypes) => { var tags = sequelize.define('tag', { id: { allowNull: false, primaryKey: true, type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, }, name: { allowNull: false, type: DataTypes.STRING, } }, {}); tags.associate = function(models) { // associations can be defined here tags.belongsToMany(models.organization, { through: 'orgTags' }) }; return tags; }; 模型

orgTags

我正在使用以下命令将数据插入表中:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var orgTags = sequelize.define('orgTags', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    organizationId: {
      allowNull: false,
      type: DataTypes.UUID,
    },
    tagId: {
      allowNull: false,
      type: DataTypes.UUID,
    }
  }, {});
  orgTags.associate = function(models) {
    // associations can be defined here
  };
  return orgTags;
};

问题在于,每次我运行代码时,它都会不断将db.organization.create({ name: 'Test Name', tags: [ { name: 'tag1' }, { name: 'tag2' } ], { include: ['tags'] } }) tag1插入我的tag2表中。

我要的是将标记插入表中不存在的tags表中,但是它应该始终将新记录插入tag表中吗? / p>

0 个答案:

没有答案