npx sequelize db:create // 데이터베이스 생성
npx sequelize db:drop // 데이터베이스 삭제
npx sequelize db:migrate // 테이블 생성 (실행 이력이 있으면 실행안된 파일을 실행 만들어줌)
npx sequelize db:migrate:undo // 마지막 테이블 삭제
npx sequelize db:migrate:undo --name [파일명] // 해당 테이블 삭제
npx sequelize db:migrate:undo:all // 테이블 전체 삭제
/**
* @file databases/migrations/20251118-07-fk-tables.js
* @description Add fk at all tables change
* 251118 v1.0.0 CK init
*/
import { DataTypes } from 'sequelize';
/** @type {import('sequelize-cli').Migration} */
export default {
// 마이그레이션을 실행 시 호출되는 메소드 (스키마 생성, 수정)
async up (queryInterface, Sequelize) {
await queryInterface.addConstraint(
'posts', // fk 생성할 테이블
{
fields: ['user_id'], // fk 부여할 컬럼
type: 'foreign key', // constraint(제약조건) 종류
nmae: 'fk_posts_user_id', // constraint명 지정
references: { // 참조 설정
table: 'users', // 참조할 테이블
field: 'id', // 참조 컬럼 지정
},
onDelete: 'CASCADE', // 참조 레코드가 작제 시, ports의 레코드도 같이 삭제
})
},
// 마이그레이션을 올백 시 호출되는 메소드 (스키마 제거, 수정)
async down (queryInterface, Sequelize) {
await queryInterface.removeConstraint('posts', 'fk_posts_user_id');
}
};