使用关联对批量插入进行续集 [英] Sequelize bulk insert with associations

查看:21
本文介绍了使用关联对批量插入进行续集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to bulk insert with associations, I have this 'Song' model which have one to many relationships with 'Genre' and 'Language' defined with the migrations CLI. Song:

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;
};

我正在尝试批量插入具有如下语言和流派的歌曲:

I'm trying to bulk insert songs with languages and genres like this:

Song.bulkCreate(songs, {
    include: [Genre,Language]
}).then(() => {
    const result = {
        status: "ok",
        message: "Upload Successfully!",
    }
    res.json(result);
});

songs 数组中的每首歌曲的结构如下:

each song in the songs array is structured like this:

{
    name: "abc",
    genres: [{name: "abc"}],
    languages: [{name: "English"}],
    energy:  1,
    valence: 1
}

我最终得到了一张完整的歌曲表,但流派和语言为空我究竟做错了什么?谢谢.

I'm ending up with a full songs table but genres and languages are empty What am I doing wrong? Thanks.

推荐答案

以防万一其他人从搜索中来到这里,从 版本 5.14Sequelize 在 bulkCreate 中添加了使用 include 选项 的选项,如下:

Just in case anyone else got here from a search, starting from version 5.14 Sequelize added the option to use include option in bulkCreate as follows:

await Song.bulkCreate(songsRecordsToCreate, {
    include: [Genre,Language]
})

这篇关于使用关联对批量插入进行续集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆