node.js - Sequelize 批量插入与关联

标签 node.js sequelize.js associations bulkinsert

我正在尝试批量插入关联,
我有这个“歌曲”模型,它与使用迁移 CLI 定义的“流派”和“语言”有一对多的关系。
歌曲:

module.exports = (sequelize, DataTypes) => {
    class Song extends Model {
     
        static associate(models) {
            // define association here
            Song.hasMany(models["Language"])
            Song.hasMany(models["Genre"])
        }
    };
    Song.init({
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        name: DataTypes.STRING,
        energy: {type: DataTypes.FLOAT, allowNull: false},
        valence: {type: DataTypes.FLOAT, allowNull: false}
    }, {
        sequelize,
        modelName: 'Song',
        timestamps: true
    });
    return Song;
};
语:
module.exports = (sequelize, DataTypes) => {
    class Language extends Model {
        static associate(models) {
            // define association here
            models["Language"].belongsTo(models["Song"])
        }
    };
    Language.init({
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        name: DataTypes.STRING
    }, {
        sequelize,
        modelName: 'Language',
        indexes: [{unique: true, fields: ['name']}]
    });
    return Language;
};
类型:
module.exports = (sequelize, DataTypes) => {
    class Genre extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method automatically.
         */
        static associate(models) {
            // define association here
            models["Genre"].belongsTo(models["Song"])
        }
    };
    Genre.init({
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        name: DataTypes.STRING
    }, {
        sequelize,
        modelName: 'Genre',
        indexes: [{unique: true, fields: ['name']}]
    });
    return Genre;
};
我正在尝试批量插入具有以下语言和流派的歌曲:
Song.bulkCreate(songs, {
    include: [Genre,Language]
}).then(() => {
    const result = {
        status: "ok",
        message: "Upload Successfully!",
    }
    res.json(result);
});
歌曲数组中的每首歌曲的结构如下:
{
    name: "abc",
    genres: [{name: "abc"}],
    languages: [{name: "English"}],
    energy:  1,
    valence: 1
}
我最终得到了一个完整的歌曲表,但流派和语言是空的
我究竟做错了什么?
谢谢。

最佳答案

不幸的是 bulkCreate 不像 include 那样支持 create 选项。
您应该在事务内的循环中使用 create

const transaction = ...
for (const song of songs) {
  await Song.create(song, {
    include: [Genre,Language]
  }, { transaction })
}
await transaction.commit()
或者你可以使用 Promise.all 来避免使用 for

关于node.js - Sequelize 批量插入与关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63922261/

相关文章:

node.js - Gulp 在启动时运行一次监视任务

node.js - 无需模板的 Yeoman 复制目录

javascript - 是否可以在没有 c 代码的情况下用纯 Javascript (nodejs) 编写同步阻塞 IO?

mysql - 在 Apollo GraphQL 中,如何设置解析器不循环值?

sequelize.js,计算关联项的数量

node.js - 如何在服务器中使用 node-postgres?

node.js - 如何在值为空时跳过列中的查询语句

mysql - Cakephp 模型关联及其所需的 mysql 查询量

mysql - Rails 属于_to 与parent_id

ruby-on-rails - Ruby on Rails - 访问 :has_many => :through association 中的数据