- migration
- promise
- Status 테이블 생성
- users에 state 값 만들고 default 값의 id 넣기
- PK- FK 연결해주기
- migration:undo
- users.state 삭제
- Status 테이블 삭제
'use strict';
const { QueryInterface } = require('sequelize');
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
await Promise.all([
queryInterface.createTable('Status',
{ stateId: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
stateName: {
type: Sequelize.STRING,
unique:true,
allowNull:false
}})
.then(()=>queryInterface.sequelize.query(`
INSERT INTO public."Status"(
"stateName")
VALUES ( 'Active');`
)),
queryInterface.addColumn("Users","state",{
type: Sequelize.INTEGER,
defaultValue:1,
allowNull:false
})
])
await queryInterface.addConstraint("Users",{
fields:["state"],
type:"foreign key",
references:{
table:"Status",
field:"stateId"
},
onDelete:"cascade",
onUpdate:"cascade"
})
},
async down (queryInterface, Sequelize) {
await queryInterface.removeColumn('Users','state')
await queryInterface.dropTable('Status')
}
};