ํฐ์คํ ๋ฆฌ ๋ทฐ
8.3 Sequelize foreignKey ์ง์ ํ๊ธฐ(cascade ์ ์ฉ)
๋๋์ด๋ฐ๐พ 2018. 9. 27. 17:28๊ฐ๋ง์ 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๋ก ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค.
'Node.js > Node.js ๊ณต๋ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
8.5 Sequelize ์ธ๋ํค๋ก ์ฐธ์กฐ ํ ์ด๋ธ ๋ฐ์ดํฐ ๊ฒ์ํ๊ธฐ (0) | 2018.11.01 |
---|---|
8.4 Sequelize ๋ก DML ๊ตฌํํด๋ณด๊ธฐ (0) | 2018.10.06 |
8.2 Sequelize ํ๊ฒฝ settingํ๊ธฐ (0) | 2018.06.28 |
8.1 Node js ORM ( Sequelize.js ) ์๊ฐ (0) | 2018.06.28 |
7. ํ์ผ ์ ๋ก๋ (multer ๋ชจ๋ ์ด์ฉ) (0) | 2018.05.26 |
- Total
- Today
- Yesterday
- NestJS
- typeorm
- node.js
- nestjs config
- nestjs module
- Promise error
- nestjs configService
- foreignkey
- typeorm ์ฐ๊ฒฐ
- ๋น๋๊ธฐ ์์ฒญ
- backend-framework
- JavaScript
- nestjs directory
- nestjs/cli
- @nestjs/config
- ์๊ณ ๋ฆฌ์ฆ
- docker mysql
- DeferredResult
- Spring Async
- node.js backend
- Promise bulk
- sequelize
- android
- Request_Limit_Exceeded
- nestjs doc
- ํ๋ก๊ทธ๋๋จธ์ค
- nestjs project
- ๊ธฐ์์ฒญAPI
- nestjs typeorm
- Spring
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |