Sequelize 튜토리얼(10)_실습용 데이터 넣기
이제 본격적으로 데이터 조회, 삽입, 수정, 삭제 등의 작업을 수행하기 전에 각 테이블에 적절한 seed 데이터를 넣겠다. 아래 과정을 순서대로 수행하면 된다.
1. Students 테이블 채우기
npx sequelize seed:generate --name Student
(타임스탬프)-Student.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Students', [
{
registrationNum: 20203718,
name: '김철수',
age: 22,
createdAt: new Date(),
updatedAt: new Date(),
},
{
registrationNum: 20208712,
name: '이나라',
age: 21,
createdAt: new Date(),
updatedAt: new Date(),
},
{
registrationNum: 20190124,
name: '이태용',
age: 23,
createdAt: new Date(),
updatedAt: new Date(),
},
{
registrationNum: 20180051,
name: '최미나',
age: 22,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Students', null);
},
};
2. ScholarshipAccounts 테이블 채우기
npx sequelize seed:generate --name ScholarshipAccount
(타임스탬프)-ScholarshipAccount.js
'use strict';
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('ScholarshipAccounts', [
{
accountNum: '110-329-204928',
validDate: '2027/04/12',
balance: 100000,
StudentId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-310-104538',
validDate: '2026/10/07',
balance: 200000,
StudentId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-875-104736',
validDate: '2027/11/12',
balance: 900000,
StudentId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-097-657567',
validDate: '2025/03/09',
balance: 2000000,
StudentId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-396-124356',
validDate: '2028/02/11',
balance: 0,
StudentId: null,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('ScholarshipAccounts', null);
},
};
3. Professors 테이블 채우기
npx sequelize seed:generate --name Professor
// (타임스탬프)-Professor.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Professors', [
{
employeeNum: 20107763,
name: '김박사',
age: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
employeeNum: 20129823,
name: '이천재',
age: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
employeeNum: 20092837,
name: '최고수',
age: 45,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Professors', null);
},
};
4. Courses 테이블 채우기
npx sequelize seed:generate --name Course
// (타임스탬프)-Course.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Courses', [
{
courseCode: 'A10293',
title: '알고리즘',
description: '기초 수업',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 1,
},
{
courseCode: 'A10296',
title: '해킹 공격과 방어',
description: '아주 흥미로운 수업',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 1,
},
{
courseCode: 'A12104',
title: 'C언어',
description: '포인터의 정수',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B23512',
title: 'Java와 객체 지향',
description: '패턴까지 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B28472',
title: '서버 부하 분산',
description: '실습과 함께 하는 재미난 수업입니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B48729',
title: 'Windows 구조와 원리',
description: '커널까지 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'B30029',
title: 'HTTP 제대로 배우기',
description: 'HTTP 역사도 다룹니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'C10029',
title: '컴퓨터 그래픽',
description: '그래픽의 기초부터 차근차근 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'C49382',
title: '리눅스 마스터하기',
description: '쉘 스크립트 숙제가 많습니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Courses', null);
},
};
5. Grades 테이블 채우기
npx seqeulize seed:generate --name Grade
// (타임스탬프)-Grade.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Grades', [
{
studentId: 1,
courseId: 1,
score: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 2,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 3,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 9,
score: 60,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 2,
score: 65,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 5,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 8,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 1,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 2,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 4,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 5,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 7,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 3,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 4,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 5,
score: 20,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Grades', null);
},
};
npx sequelize seed:generate --name Student
(타임스탬프)-Student.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Students', [
{
registrationNum: 20203718,
name: '김철수',
age: 22,
createdAt: new Date(),
updatedAt: new Date(),
},
{
registrationNum: 20208712,
name: '이나라',
age: 21,
createdAt: new Date(),
updatedAt: new Date(),
},
{
registrationNum: 20190124,
name: '이태용',
age: 23,
createdAt: new Date(),
updatedAt: new Date(),
},
{
registrationNum: 20180051,
name: '최미나',
age: 22,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Students', null);
},
};
npx sequelize seed:generate --name ScholarshipAccount
(타임스탬프)-ScholarshipAccount.js
'use strict';
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('ScholarshipAccounts', [
{
accountNum: '110-329-204928',
validDate: '2027/04/12',
balance: 100000,
StudentId: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-310-104538',
validDate: '2026/10/07',
balance: 200000,
StudentId: 2,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-875-104736',
validDate: '2027/11/12',
balance: 900000,
StudentId: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-097-657567',
validDate: '2025/03/09',
balance: 2000000,
StudentId: 4,
createdAt: new Date(),
updatedAt: new Date(),
},
{
accountNum: '110-396-124356',
validDate: '2028/02/11',
balance: 0,
StudentId: null,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('ScholarshipAccounts', null);
},
};
3. Professors 테이블 채우기
npx sequelize seed:generate --name Professor
// (타임스탬프)-Professor.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Professors', [
{
employeeNum: 20107763,
name: '김박사',
age: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
employeeNum: 20129823,
name: '이천재',
age: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
employeeNum: 20092837,
name: '최고수',
age: 45,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Professors', null);
},
};
4. Courses 테이블 채우기
npx sequelize seed:generate --name Course
// (타임스탬프)-Course.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Courses', [
{
courseCode: 'A10293',
title: '알고리즘',
description: '기초 수업',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 1,
},
{
courseCode: 'A10296',
title: '해킹 공격과 방어',
description: '아주 흥미로운 수업',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 1,
},
{
courseCode: 'A12104',
title: 'C언어',
description: '포인터의 정수',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B23512',
title: 'Java와 객체 지향',
description: '패턴까지 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B28472',
title: '서버 부하 분산',
description: '실습과 함께 하는 재미난 수업입니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B48729',
title: 'Windows 구조와 원리',
description: '커널까지 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'B30029',
title: 'HTTP 제대로 배우기',
description: 'HTTP 역사도 다룹니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'C10029',
title: '컴퓨터 그래픽',
description: '그래픽의 기초부터 차근차근 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'C49382',
title: '리눅스 마스터하기',
description: '쉘 스크립트 숙제가 많습니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Courses', null);
},
};
5. Grades 테이블 채우기
npx seqeulize seed:generate --name Grade
// (타임스탬프)-Grade.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Grades', [
{
studentId: 1,
courseId: 1,
score: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 2,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 3,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 9,
score: 60,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 2,
score: 65,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 5,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 8,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 1,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 2,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 4,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 5,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 7,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 3,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 4,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 5,
score: 20,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Grades', null);
},
};
npx sequelize seed:generate --name Professor
// (타임스탬프)-Professor.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Professors', [
{
employeeNum: 20107763,
name: '김박사',
age: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
employeeNum: 20129823,
name: '이천재',
age: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
employeeNum: 20092837,
name: '최고수',
age: 45,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Professors', null);
},
};
npx sequelize seed:generate --name Course
// (타임스탬프)-Course.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Courses', [
{
courseCode: 'A10293',
title: '알고리즘',
description: '기초 수업',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 1,
},
{
courseCode: 'A10296',
title: '해킹 공격과 방어',
description: '아주 흥미로운 수업',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 1,
},
{
courseCode: 'A12104',
title: 'C언어',
description: '포인터의 정수',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B23512',
title: 'Java와 객체 지향',
description: '패턴까지 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B28472',
title: '서버 부하 분산',
description: '실습과 함께 하는 재미난 수업입니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 2,
},
{
courseCode: 'B48729',
title: 'Windows 구조와 원리',
description: '커널까지 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'B30029',
title: 'HTTP 제대로 배우기',
description: 'HTTP 역사도 다룹니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'C10029',
title: '컴퓨터 그래픽',
description: '그래픽의 기초부터 차근차근 배웁니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
{
courseCode: 'C49382',
title: '리눅스 마스터하기',
description: '쉘 스크립트 숙제가 많습니다',
createdAt: new Date(),
updatedAt: new Date(),
ProfessorId: 3,
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Courses', null);
},
};
5. Grades 테이블 채우기
npx seqeulize seed:generate --name Grade
// (타임스탬프)-Grade.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Grades', [
{
studentId: 1,
courseId: 1,
score: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 2,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 3,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 9,
score: 60,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 2,
score: 65,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 5,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 8,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 1,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 2,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 4,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 5,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 7,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 3,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 4,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 5,
score: 20,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Grades', null);
},
};
npx seqeulize seed:generate --name Grade
// (타임스탬프)-Grade.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Grades', [
{
studentId: 1,
courseId: 1,
score: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 2,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 3,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 1,
courseId: 9,
score: 60,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 2,
score: 65,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 5,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 2,
courseId: 8,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 1,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 2,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 4,
score: 80,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 5,
score: 100,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 3,
courseId: 7,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 3,
score: 40,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 4,
score: 90,
createdAt: new Date(),
updatedAt: new Date(),
},
{
studentId: 4,
courseId: 5,
score: 20,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Grades', null);
},
};
지금 여기 나온 순서대로 seed 파일을 생성해주세요. 이 순서를 지키지 않으면 아직 생성되지 않은 테이블의 row가 마치 존재하는 것처럼 외래키 값이 삽입되는 경우 에러가 발생합니다.
모든 작업을 수행했으면
node app.js
를 실행해서 데이터베이스를 초기화 후 테이블 생성 및 외래키 설정을 하고
npx seqeulize db:seed:all
을 실행해서 seeders 디렉토리 안의 모든 seed 파일들이 적용되도록 해주세요. 그리고 나면
Students 테이블
ScholarshipAccounts 테이블
Professors 테이블
Courses 테이블
Grades 테이블
이렇게 seed 데이터들이 채워진 것을 볼 수 있습니다.
Author And Source
이 문제에 관하여(Sequelize 튜토리얼(10)_실습용 데이터 넣기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@usaindream/Sequelize-튜토리얼10실습용-데이터-넣기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)