序列化belongsToMany会忽略联接表中的其他属性

时间:2019-02-07 16:19:23

标签: node.js postgresql sequelize.js has-and-belongs-to-many

我有两个模型:通过连接表描述 BelongsToMany 关联中的 Article DescriptionFragment 依次属于 另一个模型类别,并且还具有“序列”属性,所有属性定义如下:

文章模型:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Article = sequelize.define('Article', {
    uid: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      field: 'uid'
    },
    articleNumber: {
      type: DataTypes.INTEGER(8),
      allowNull: false,
      field: 'article_number',
      unique: true
    },
  }, {
    underscored: true,
  });
  Article.associate = function (models) {
    Article.belongsToMany(models.DescriptionFragment, {
      through: 'Descriptions',
      as: 'articleWithDescriptionFragment',
      otherKey: {
        name: 'descriptionFragmentId',
        field: 'description_fragment_id'
      },
      foreignKey: {
        name: 'articleId',
        field: 'article_id'
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    });
  };
  return Article;
};

DescriptionFragment 模型:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const DescriptionFragment = sequelize.define('DescriptionFragment', {
    uid: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      field: 'uid'
    }
  }, {
    charset: 'utf8',
    dialectOptions: {
      collate: 'utf8_general_ci'
    },
    timestamps: true,
    paranoid: true,
    underscored: true,
  });
  DescriptionFragment.associate = function (models) {
    DescriptionFragment.belongsToMany(models.Article, {
      through: 'Descriptions',
      as: 'descriptionFragmentForArticle',
      foreignKey: {
        name: 'descriptionFragmentId',
        field: 'description_fragment_id'
      },
      otherKey: {
        name: 'articleId',
        field: 'article_id'
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    });
  };
  return DescriptionFragment;
};

说明模型:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Description = sequelize.define('Description', {
    sequence: {
      type: DataTypes.INTEGER,
      allowNull: true
    }
  }, {
    charset: 'utf8',
    dialectOptions: {
      collate: 'utf8_general_ci'
    },
    timestamps: true,
    paranoid: true,
    underscored: true,
  });
  Description.associate = function (models) {
    Description.belongsTo(models.Category, {
      as: 'categoryDescription',
      foreignKey: {
        name: 'categoryId',
        field: 'category_id'
      },
      targetKey: 'uid',
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    });
  };
  return Description;
};

类别模型:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Category = sequelize.define('Category', {
    uid: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      field: 'uid'
    }
  }, {
    charset: 'utf8',
    dialectOptions: {
      collate: 'utf8_general_ci'
    },
    timestamps: true,
    paranoid: true,
    underscored: true,
  });
  Category.associate = function (models) {
    Category.hasMany(models.Description, {
      foreignKey: {
        name: 'categoryId',
        field: 'category_id'
      },
      sourceKey: 'uid',
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    });
  };
  return Category;
};

当我尝试将 DescriptionFragment Article 相关联时,我的问题就出现了。我正在使用以下代码:

let addedArticle = await Article.create(newArticle);

addedArticle.addArticleWithDescriptionFragment(descFrag, {
    through: {
      categoryId: catId,
      sequence: index
    }
  });

其中 descFrag DescriptionFragment 模型的实例,而 catId index 是整数。

运行该代码时,sequelize创建 Article 实例 addedArticle ,但是当尝试将其与 DescriptionFragment 关联时,它只是忽略了什么在 through 选项中。生成的SQL例如:

Executing (default): INSERT INTO "devdb"."Descriptions" ("created_at","updated_at","article_id","description_fragment_id") VALUES ('2019-02-07 15:11:15.376 +00:00','2019-02-07 15:11:15.376 +00:00',95,5);

据我在documentation中所发现的,我使用的语法是正确的,并且该关联是在 Descriptions 表中创建的,仅使用null表示序列 category_id

我正在使用sequelize v4.38.1,数据库是Postgres。

我无法确定错误在哪里,到目前为止,我发现的所有类似问题都只是在v4的后续版本中使用了v3的旧语法。

任何见识将不胜感激,谢谢!


更新

目前,我正在使用以下解决方法:

await addedArticle.addArticleWithDescriptionFragment(descFrag);
let newDesc = await models.Description.find({
  where: { articleId: addedArticle.uid, descriptionFragmentId: descFrag.uid }
});
await newDesc.update({ categoryId: catId, sequence: index });

可以正确设置所需的列:

Executing (default): UPDATE "techred_dev"."Descriptions" SET "category_id"=2,"sequence"=0,"updated_at"='2019-02-08 09:21:20.216 +00:00' WHERE "article_id" = 104 AND "description_fragment_id" = 6

当然要执行此操作,我必须更新我的 Description 模型,明确添加 articleId descriptionFragmentId 列:

articleId: {
  type: DataTypes.INTEGER,
  onDelete: 'CASCADE',
  onUpdate: 'CASCADE',
  references: {
    model: 'Articles',
    key: 'uid'
  },
  primaryKey: true,
  field: 'article_id'
},
descriptionFragmentId: {
  type: DataTypes.INTEGER,
  onDelete: 'CASCADE',
  onUpdate: 'CASCADE',
  references: {
    model: 'DescriptionFragments',
    key: 'uid'
  },
  primaryKey: true,
  field: 'description_fragment_id'
},

仍然,'add'方法中的'through'选项不起作用,我也不知道为什么。

0 个答案:

没有答案