8.3 Sequelize foreignKey μ§μ νκΈ°(cascade μ μ©)
κ°λ§μ Sequelizeλ₯Ό μ λ°μ΄νΈνκ² λμμ΅λλ€. μ§λλ²μλ ν μ΄λΈ μ μκΉμ§λ§ ν΄λ³΄μλλ° ν μ΄λΈ μ μ μ€ μΈλν€μ κ΄ν κ±Έ ν¬μ€ν νλ €κ³ ν©λλ€. μλ¬΄λ° κ΄κ³κ° μλ λ 립μ μΈ ν μ΄λΈμ μ μνλ건 무μλ―Έν κ²½μ°κ° λ§κΈ°λλ¬Έμ μ΄λΆλΆμ 곡λΆνκ² λμμ΅λλ€. μΈλν€μ μ μμ κ°μ μμΈν κ°λ μ μλ΅νκ³ λμ΄κ°κ² μ΅λλ€.
μΌλ¨ μ ν μ΄λΈμ μ μλ μ΄λ κ² λμ΄μμ΅λλ€.
μΌλ¨ ν μ΄λΈ μ μλ₯Ό ν jsνμΌμ μ΄ν΄λ³΄κ² μ΅λλ€.
[user.js]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | var models = require('../models'); module.exports = function (sequelize, DataTypes) { const user = sequelize.define('User', { name: { field: 'name', type: DataTypes.STRING(20), allowNull:false }, nick: { field: 'nick', type: DataTypes.STRING(20), allowNull:false }, user_id: { field: 'user_id', type: DataTypes.STRING(50), allowNull: false, unique: true }, password: { field: 'password', type: DataTypes.STRING(30), allowNull:false }, }, { // don't use camelcase for automatically added attributes but underscore style // so updatedAt will be updated_at charset: 'utf8', collate: 'utf8_unicode_ci', underscored: true, // disable the modification of tablenames; By default, sequelize will automatically // transform all passed model names first parameter of define) into plural. // if you don't want that, set the following freezeTableName: true, // define the table's name tableName: 'user' }); user.associate = function(models) { models.User.hasMany(models.Code, { foreignKey: 'fk_userId', onDelete: 'cascade' }); }; return user; }; | cs |
[code.js]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | module.exports = function (sequelize, DataTypes) { const code = sequelize.define('Code', { title: { field: 'title', type: DataTypes.STRING(10), allowNull: false }, code: { field: 'code', type: DataTypes.STRING(100), allowNull: false }, }, { // don't use camelcase for automatically added attributes but underscore style // so updatedAt will be updated_at underscored: true, // disable the modification of tablenames; By default, sequelize will automatically // transform all passed model names (first parameter of define) into plural. // if you don't want that, set the following freezeTableName: true, // define the table's name tableName: 'code' }); return code; }; | cs |
λ ν μ΄λΈ μ μλ¬Έμ 보면 ꡬ쑰λ κ±°μ λΉμ·νλ° user ν μ΄λΈ νλ¨μ
1 2 3 4 5 6 | user.associate = function(models) { models.User.hasMany(models.Code, { foreignKey: 'fk_userId', onDelete: 'cascade' }); }; | cs |
μ΄μκ°μ΄ μμ±λμ΄μμ΅λλ€. μ΄λ κ² λλ©΄ codeν μ΄λΈμ fk_userId λΌλ μλ‘μ΄ νλκ° μκΉλλ€. fk_userId λΌλ νλλ userν μ΄λΈμ primary key μ λμΌν μμ±μ κ°μ§λ νλλ‘ μ μλλ©°, μ¬κΈ°μλ userν μ΄λΈμ idνλλ₯Ό κ°λ¦¬ν΅λλ€. onDelete λΌλκ²μ user ν μ΄λΈμ idνλ κ°μ΄ μμ λμμ λ code ν μ΄λΈμμ fk_userId νλκ° λμΌν idκ°μ κ°λ¦¬ν€κ³ μλ λ°μ΄ν°λ€μ μΌκ΄μ μΌλ‘ μμ κ° λ©λλ€.
μλ₯Όλ€λ©΄ AλΌλ νμμ΄ κΈμ 3κ° μ»λλ° AλΌλ νμμ΄ νμνν΄λ₯Ό νκ² λλ€λ©΄ Aκ° μ΄ κΈμ λ€ μ§μμ ΈμΌ νλ κ²μ²λΌ λ§μ΄μ£ .
κ·Έλ¦ΌμΌλ‘ ννν΄λ³΄μλ©΄ μ΄λ° μνμ λλ€.
sqlμ delete λ¬Έμ μ¨λ³΄κ² μ΅λλ€.
id κ° 2μΈ userν μ΄λΈμ λ°μ΄ν° ν νμ μμ νλλ code ν μ΄λΈμ μλ λ°μ΄ν°λ€μ΄ λͺ¨λ μμ κ° λ κ²μ λ³Ό μ μμ΅λλ€.code ν μ΄λΈμ μλ λ°μ΄ν°λ€μ λͺ¨λ userν μ΄λΈμ idκ° 2μΈ μ¬μ©μλ₯Ό κ°λ¦¬ν€κ³ μλ λ°μ΄ν°μλ κ²μ΄μ£ .
μ§κΈκΉμ§λ ν μ΄λΈ μ μλ§ jsνμΌμ μ΄μ©ν΄ ꡬννκ³ λ°μ΄ν° μ½μ μ΄λ μμ λ SQLλ¬Έμ ν΅ν΄ μ€μ΅νμμ΅λλ€. ν μ΄λΈ μ μλΆλΆμ΄ μΌμΆ μ 리λλ©΄ μ΄νμλ λ°μ΄ν°μ‘°μμ΄ (INSERT, UPDATE, DELETE) μ κ°μ λ¬Έλ²λ jsλ‘ κ΅¬νν΄λ³΄κ² μ΅λλ€.