我有两个模型,例如课程和视频。课程有很多视频。
// course.js
'use strict';
module.exports = (sequelize, DataTypes) => {
const Course = sequelize.define('Course', {
title: DataTypes.STRING,
description: DataTypes.STRING
});
Course.associate = models => {
Course.hasMany(models.Video);
};
return Course;
};
// video.js
'use strict';
module.exports = (sequelize, DataTypes) => {
const Video = sequelize.define('Video', {
title: DataTypes.STRING,
description: DataTypes.STRING,
videoId: DataTypes.STRING
});
Video.associate = models => {
Video.belongsTo(models.Course, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
})
};
return Video;
};
我想用包含视频的课程创建种子。我该怎么做?我不知道如何使用附带的视频创建种子。
答案 0 :(得分:12)
您可以使用Sequelize的queryInterface
下拉到原始SQL,以插入需要关联的模型实例。在您的情况下,最简单的方法是为课程和视频创建一个播种机。 (注意:我不知道你是如何定义主键和外键的,所以我假设视频表有一个字段course_id
。)
module.exports = {
up: async (queryInterface) => {
await queryInterface.bulkInsert('courses', [
{title: 'Course 1', description: 'description 1', id: 1}
{title: 'Course 2', description: 'description 2', id: 2}
], {});
const courses = await queryInterface.sequelize.query(
`SELECT id from COURSES;`
);
const courseRows = courses[0];
return await queryInterface.bulkInsert('videos', [
{title: 'Movie 1', description: '...', id: '1', course_id: courseRows[0].id}
{title: 'Movie 2', description: '...', id: '2', course_id: courseRows[0].id},
{title: 'Movie 3', description: '...', id: '3', course_id: courseRows[0].id},
], {});
},
down: async (queryInterface) => {
await queryInterface.bulkDelete('videos', null, {});
await queryInterface.bulkDelete('courses', null, {});
}
};
答案 1 :(得分:0)
bulkInsert
方法返回使用第一个插入项的ID解析的Promise。我们可以使用此信息来插入视频。看起来可能像这样:
function getId( firstId, items, needly ) {
for ( let i = 0; i < items.length; i++ ) {
if ( items[i].title === needly ) {
return firstId + i;
}
}
return null;
}
exports.up = async ( queryInterface, Sequelize ) => {
const courses = [
{
title: 'Course 1',
description: '...',
},
{
title: 'Course 2',
description: '...',
},
{
title: 'Course 3',
description: '...',
},
{
title: 'Course 4',
description: '...',
},
{
title: 'Course 5',
description: '...',
},
];
const firstId = await queryInterface.bulkInsert( 'courses', courses, {} );
const course2Id = getId( firstId, courses, 'Course 2' );
const course5Id = getId( firstId, courses, 'Course 5' );
return queryInterface.bulkInsert( 'categories', [
{ title: 'Video 1', description: '...', courseId: course2Id },
{ title: 'Video 2', description: '...', courseId: course2Id },
{ title: 'Video 3', description: '...', courseId: course5Id },
{ title: 'Video 4', description: '...', courseId: course5Id },
{ title: 'Video 5', description: '...', courseId: course5Id },
], {} );
};
exports.down = async ( queryInterface ) => {
await queryInterface.bulkDelete( 'videos', null, {} );
await queryInterface.bulkDelete( 'courses', null, {} );
}
答案 2 :(得分:0)
在{ returning: true }
的选项字段中传递bulkInsert
时,它将返回创建的对象。
let createdOjects = await queryInterface.bulkInsert("table_name", data_to_be_inserted, { returning: true });
此外,您可以传递一个数组,其中包含您感兴趣的字段,例如ID { returning: ['id'] }
,这将返回所创建对象的ID数组
let createdIds = await queryInterface.bulkInsert("table_name", data_to_be_inserted, { returning: ['id'] });
您还可以遍历返回的对象/标识,并使用bulkInsert
插入嵌套的对象。
示例代码:
module.exports = {
up: async (queryInterface) => {
let courses = ["..."]
let videos = ["..."]
let videoIds = await queryInterface.bulkInsert("courses", courses, { returning: ["id"] });
//add courseId to each video object -- depends on your scheme
await queryInterface.bulkInsert("videos", videos);
},
down: async (queryInterface) => {
await queryInterface.bulkDelete("videos", null, {});
await queryInterface.bulkDelete("courses", null, {});
},
};