Loading...
2022. 6. 26. 15:50

[Spring] OSIV๋กœ ์•Œ์•„๋ณด๋Š” Spring Transaction ํ—ค์งš๊ธฐ

Spring boot์—๋Š” spring.jpa.open-in-view๋ผ๊ณ  ํ•˜๋Š” ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์€ JPA์˜ OSIV ๊ธฐ๋Šฅ์„ ON/OFF ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์ธ๋ฐ์š”. ์ด ์˜ต์…˜์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. OSIV ๋จผ์ € ์ด ์˜ต์…˜์€ JPA์˜ OSIV ๊ธฐ๋Šฅ์˜ ์‚ฌ์šฉ ์œ ๋ฌด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ, OSIV๋ž€, ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ View๊นŒ์ง€ ์—ด์–ด๋‘๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ž˜ ๋ชจ๋ฅด์‹œ๊ฒ ๋‹ค๋ฉด ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•ด๋ณด์„ธ์š”. 2020.06.23 - [Programming/Spring] - [Spring] JPA์˜ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์™€ ์ƒ๋ช…์ฃผ๊ธฐ [Spring] JPA์˜ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์™€ ์ƒ๋ช…์ฃผ๊ธฐ Spring Framework๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋‚˜ ์„œ๋ฒ„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” DB ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค..

2022. 6. 22. 21:33

[Programming] SOLID - ๊ฐ์ฒด ์ง€ํ–ฅ 5๋Œ€ ์„ค๊ณ„ ์›์น™

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ํŠน์ • ์–ธ์–ด์— ๋Œ€ํ•œ ํ•™์Šต์ด ์•„๋‹Œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์Šคํ‚ฌ์— ๋Œ€ํ•ด ์ ์–ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋…์ž ์—ฌ๋Ÿฌ๋ถ„๋“ค ์ค‘ Java, C#, Python๊ณผ ๊ฐ™์€ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ณ  ๊ณ„์‹ ๋‹ค๋ฉด OOP(๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์‰ฝ๊ฒŒ ์ ‘ํ•ด๋ณด์…จ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. OOP (Object Oriented Programming) OOP๋Š” ์šฐ๋ฆฌ ๋ง๋กœ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ‚ค์—์„œ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „ ko.wikipedia.org ์‰ฝ๊ฒŒ ์–˜๊ธฐํ•˜์ž๋ฉด ์šฐ๋ฆฌ๊ฐ€ C ์–ธ์–ด๋กœ ์ž‘์„ฑ๋˜์—ˆ๋˜ ๋ช…๋ น ํ˜•์‹์˜ ์ ˆ์ฐจ ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ๋Š” ๋‹ฌ๋ฆฌ OOP๋Š” ๊ฐ์ฒด๋ผ๋Š” ๋…๋ฆฝ๋œ ๋‹จ์œ„๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ํ˜•์„ฑ์‹œํ‚ค๋Š” ์Šคํ‚ฌ์ž…๋‹ˆ๋‹ค. ์ ˆ์ฐจ ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ '์ ˆ..

2022. 5. 29. 18:18

[Algorithm] - 2022 ์นด์นด์˜ค ์‹ ์ž… 1์ฐจ ์˜จ๋ผ์ธ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ (Kakao Newbie Primary Online Coding Test) for Tech developers ๋ฌธ์ œ ํ’€์ด

๋งค๋…„ ์ด๋ค„์ง€๋Š” ์นด์นด์˜ค ์‹ ์ž… ์˜จ๋ผ์ธ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ ํ’€์ด๋ฅผ ๋˜ ์ ๊ฒŒ ๋˜์—ˆ๋„ค์š”. ํšŒ์‚ฌ ์—…๋ฌด๋ฅผ ์ข€ ๋” ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋‹ค๋ณด๋‹ˆ ๋งˆ๋ƒฅ ํ‘ธ๋Š” ๊ฒƒ์„ ๋ฏธ๋ค„๋†“๊ณ ๋งŒ ์žˆ๋‹ค ์ด์ œ์„œ์•ผ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋„ ๋จผ์ € ๋ฌธ์ œ ํ’€์ด ์ „ ํ›„๊ธฐ๋ฅผ ์ž ๊น ๋ง์”€๋“œ๋ฆฌ์ž๋ฉด ์ž‘๋…„ ๋ฌธ์ œ์— ๋น„ํ•˜์—ฌ ๋‚œ์ด๋„๊ฐ€ ๋งŽ์ด ๊ฐœ์„ ๋œ ๋ชจ์Šต์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ž‘๋…„๊ณผ ๋‹ฌ๋ฆฌ Lv 4 ๋ฌธ์ œ๋Š” ์ถœ์ œ ๋˜์ง€ ์•Š์•˜๊ณ , ๊ตฌํ˜„ ์ค‘์‹ฌ์˜ ์•ž ๋ฌธ์ œ์™€ ์ข€ ๋” ์‹ค๋ ฅ์„ ๋ณด๊ธฐ ์œ„ํ•œ Lv 3 ๋‚œ์ด๋„ ๋ฌธ์ œ 1~2๋ฌธ์ œ๋กœ ์ถ”๋ ค์ง„ ๋ชจ์Šต์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Question 01. ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ ๋ฌธ์ œ ์›๋ฌธ: (https://programmers.co.kr/learn/courses/30/lessons/92334) ์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ์˜ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”..

2022. 5. 22. 21:13

[FastAPI] 16. Pulumi๋ฅผ ์ด์šฉํ•ด FastAPI Serverless ํ™˜๊ฒฝ ๊ตฌ์ถ•ํ•ด๋ณด๊ธฐ

์šฐ๋ฆฌ๋Š” ์ง€๋‚œ ๊ธ€์—์„œ FastAPI, Magnum, AWS Serverless Stack(Lambda, API Gateway)์„ ์ด์šฉํ•˜์—ฌ FastAPI ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ–ˆ์Šต๋‹ˆ๋‹ค. 2022.05.22 - [Programming/Python] - [FastAPI] 15. FastAPI๋ฅผ Serverless๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• - Mangum [FastAPI] 15. FastAPI๋ฅผ Serverless๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• - Mangum ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” FastAPI์™€ Serverless(์„œ๋ฒ„๋ฆฌ์Šค) ์กฐํ•ฉ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ดˆ๊ธฐ ์„œ๋ฒ„๋ฆฌ์Šค๋ฅผ ๋ฐฐ์› ์„ ๋•Œ๋Š” AWS์˜ Lambda๋ผ๋Š” ์„œ๋น„์Šค๊ฐ€ ๋‚˜์™”์„ ๋•Œ์˜€๋Š”๋ฐ์š”. ๋‹น์‹œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ณ„๋„์˜ ํ”„ blog.neonkid.xyz ํ•˜์ง€๋งŒ AWS Lambda์™€ API..

2022. 5. 22. 00:03

[FastAPI] 15. FastAPI๋ฅผ Serverless๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• - Mangum

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” FastAPI์™€ Serverless(์„œ๋ฒ„๋ฆฌ์Šค) ์กฐํ•ฉ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ดˆ๊ธฐ ์„œ๋ฒ„๋ฆฌ์Šค๋ฅผ ๋ฐฐ์› ์„ ๋•Œ๋Š” AWS์˜ Lambda๋ผ๋Š” ์„œ๋น„์Šค๊ฐ€ ๋‚˜์™”์„ ๋•Œ์˜€๋Š”๋ฐ์š”. ๋‹น์‹œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ณ„๋„์˜ ํ”„๋ ˆ์ž„์›Œํฌ ํ•„์š”์—†์ด ํ•จ์ˆ˜์˜ ๊ตฌํ˜„๋งŒ์œผ๋กœ API ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” Cloud Native(ํด๋ผ์šฐ๋“œ ๋„ค์ดํ‹ฐ๋ธŒ) ๊ฐœ๋ฐœ ๋ชจ๋ธ์ด์—ˆ์Šต๋‹ˆ๋‹ค. Serverless Serverless(์„œ๋ฒ„๋ฆฌ์Šค)๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด์„ํ•˜๋ฉด '์„œ๋ฒ„ ์—†๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜', '์„œ๋ฒ„ ์—†๋Š” ์„œ๋น„์Šค'๋ผ๋Š” ๋œป์ด ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์„œ๋ฒ„๋ฆฌ์Šค๋Š” ์„œ๋ฒ„ ์—†์ด ์„œ๋น„์Šค๋ฅผ ์šด์˜ํ•˜๊ฑฐ๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์‹ค์ œ ์„œ๋ฒ„๋ฆฌ์Šค๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ณ„๋„๋กœ ์„œ๋ฒ„ ๊ด€๋ฆฌ๋ผ๋Š” ๋น„์šฉ์—†์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฐœ๋ฐœ ๋ชจ๋ธ๋กœ์จ ๋Œ€ํ‘œ์ ์œผ..

2022. 5. 16. 22:06

[Spring] Spring Advice๋กœ ์ปค์Šคํ…€ ์–ด๋“œ๋ฐ”์ด์Šค ๋งŒ๋“ค๊ธฐ

์ด๋ฒˆ ํฌ์ŠคํŠธ๋ถ€ํ„ฐ๋Š” Spring AOP ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹ค์ œ Advice๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ณ , ์œ„๋น™ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Advice Interface Advice์— ๋Œ€ํ•ด ๋‹ค์‹œ ๋ณต์Šตํ•ด๋ณด์ž๋ฉด Advice๋Š” ํŠน์ • ์กฐ์ธํฌ์ธํŠธ ์ฆ‰, ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์— ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ •์˜ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ProxyFactory ํด๋ž˜์Šค์—์„œ addAdvice ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์–ด๋“œ๋ฐ”์ด์Šค๋ฅผ ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ Advisor ๊ตฌํ˜„์ฒด๋ฅผ addAdvice์— ์ „๋‹ฌํ•˜์—ฌ ๊ฐ„์ ‘์ ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ์ฃ . ๋‘ ๋ฐฉ๋ฒ•์— ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์–ด๋“œ๋ฐ”์ด์Šค์™€ ๋‹ฌ๋ฆฌ Advisor๋Š” ๊ด€๋ จ PointCut๊ณผ ํ•จ๊ป˜ ์–ด๋“œ๋ฐ”์ด์Šค๋ฅผ ์ „๋‹ฌํ•œ๋‹ค๋Š” ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. PointCut์€ ์‹ค์ œ ์–ด๋“œ๋ฐ”์ด์Šค๋ฅผ ์–ธ์ œ ์‹คํ–‰ํ• ์ง€๋ฅผ ์ •ํ•˜๋Š” AOP ์šฉ์–ด๋กœ ์–ด๋“œ๋ฐ”์ด์Šค๋ฅผ ์‹คํ–‰ํ•  ์ง€์ ์„ ์ƒ์„ธํ•˜๊ฒŒ ์ง€์ •..

2022. 5. 15. 22:37

[Spring] Spring AOP - Spring์—์„œ๋Š” AOP๋ฅผ ์–ด๋–ป๊ฒŒ ์ด์šฉํ• ๊นŒ?

์•ž์„œ ์šฐ๋ฆฌ๋Š” AOP(Aspect Oriented Programming, ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)์— ๋Œ€ํ•œ ๊ธฐ๋ณธ๊ณผ ๊ฐœ๋…์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ฝ์–ด๋ณด์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ์ด ๊ธ€์„ ์ฝ๊ธฐ ์ „ ๋ฐ˜๋“œ์‹œ ์ฝ์–ด๋ณด์‹œ๊ธธ ๊ถŒ์žฅ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 2022.05.14 - [Programming/Spring] - [Spring] AOP (Aspect-Oriented Programming) ๊ธฐ๋ณธ๊ณผ ๊ฐœ๋… [Spring] AOP (Aspect-Oriented Programming) ๊ธฐ๋ณธ๊ณผ ๊ฐœ๋… Spring์—๋Š” AOP(Aspect-Oriented Programming, ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)์ด๋ผ๋Š” ํ•ต์‹ฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์Œ? OOP(๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)๋ผ๋Š” ๊ฒƒ์€ ๋“ค์–ด๋ดค๋Š”๋ฐ, AOP๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? AOP vs OOP ? ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ blog..

2022. 5. 14. 20:21

[Spring] AOP (Aspect-Oriented Programming) ๊ธฐ๋ณธ๊ณผ ๊ฐœ๋…

Spring์—๋Š” AOP(Aspect-Oriented Programming, ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)์ด๋ผ๋Š” ํ•ต์‹ฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์Œ? OOP(๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)๋ผ๋Š” ๊ฒƒ์€ ๋“ค์–ด๋ดค๋Š”๋ฐ, AOP๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? AOP vs OOP ? ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(AOP)์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ฝ”๋“œ๊ฐ€ ์ค‘๋ณต๋˜๊ณ  ๊ฐ•๋ ฅํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ๋‹ค๋ฅธ ๋กœ์ง๊ณผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง๋“ค์„ ๋ณ„๋„์˜ ๊ด€์‹ฌ์‚ฌ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ชจ๋“ˆํšŒ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ณดํ†ต ์ด๋Ÿฌํ•œ ๋กœ์ง์„ ๋งŒ๋‚ฌ์„ ๋•Œ ์ฝ”๋“œ๋ฅผ ๋ณต์ œํ•˜์—ฌ ์˜์กด์„ฑ์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋Š”๋ฐ, AOP๋Š” ์ด๋Ÿฌํ•œ ์˜์กด์„ฑ ์—†์ด๋„ ๋™์ž‘ํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ logging ์ฝ”๋“œ๋“ค์ด ์—ฌ๊ธฐ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด AOP์™€ OOP๋Š” ์„œ๋กœ ๊ฒฝ์Ÿํ•˜๋Š” ๊ด€๊ณ„์ธ๊ฐ€์š”? ๊ทธ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” OOP๋งŒ์„ ๊ฐ€์ง€๊ณ  ..

2022. 5. 5. 20:30

[Spring Data] @Transactional ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋ณด๋Š” Spring์˜ ํŠธ๋žœ์žญ์…˜ ์ด์•ผ๊ธฐ

Spring์„ ํ•œ ๋ฒˆ ์ฏค ์‚ฌ์šฉํ•ด๋ณด์…จ๋‹ค๋ฉด Transactional ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ์•Œ๊ณ ๊ณ„์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฌด์ž‘์ • ์ผ์„ ๋• DB๋ž‘ ์—ฐ๊ฒฐํ•˜๊ณ  ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์˜์†ํ•  ๋•Œ ์ด๊ฑฐ ์“ฐ๋ฉด ๋ผ. ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด ๊ณ„์…จ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๊ทธ๋ ‡์ง€ ์•Š์•˜์„ ๋•Œ ์™œ ์ด๋Ÿฐ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ ์ฏค ๊ถ๊ธˆ์ฆ์„ ๊ฐ€์ ธ๋ณด์‹  ๋ถ„๋“ค์ด ๊ณ„์‹ ๋‹ค๋ฉด ์ด ๊ธ€์„ ์ฐจ๋ถ„ํžˆ ์ฝ์–ด ๋ณด์…จ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. Transaction ํŠธ๋žœ์žญ์…˜์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค๋ฅธ ๊ธ€์—์„œ๋„ ์ถฉ๋ถ„ํžˆ ์–ธ๊ธ‰๋˜์–ด ์žˆ๋Š” ๋‚ด์šฉ์ด์ง€๋งŒ ์—ฌ๊ธฐ์„œ ํ•œ ๋ฒˆ ๋” ์„ค๋ช…์„ ๋“œ๋ฆฌ๋ฉด, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํŠธ๋žœ์žญ์…˜์„ ์ด์•ผ๊ธฐํ•˜๋ฉฐ DBMS ํ˜น์€ ๊ทธ ์œ ์‚ฌํ•œ ์‹œ์Šคํ…œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์—ฐ์‚ฐ๋“ค์˜ ์ƒํ˜ธ์ž‘์šฉ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…์„ ํ•ด๋ณด๋ฉด, ์šฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ ํ•˜๋‚˜๋ฅผ ์˜์†ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„..

2022. 4. 24. 17:49

[FastAPI] 14. SQLAlchemy์˜ One-to-Many, Many-to-Many, Self referential relationship

์ง€๋‚œ ํฌ์ŠคํŠธ์—์„œ๋Š” ๋‹จ์ˆœ Column ๋ฐ์ดํ„ฐ์™€ ๊ทธ๋ฆฌ๊ณ , ๋‹จ์ผ ์™ธ๋ž˜ํ‚ค ํ•˜๋‚˜์˜ ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ Pydantic์œผ๋กœ ๋งคํ•‘ํ•˜์—ฌ Serialize/Deserialize ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” Many-to-Many, One-to-Many ํ˜•ํƒœ์˜ Foreign Key๊ฐ€ ๊ฑธ๋ ค ์žˆ๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ Joinํ•ด์„œ ๊ฐ€์ ธ์˜ฌ ๋–„ ์–ด๋–ป๊ฒŒ Pydantic ๋ชจ๋ธ๊ณผ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. DB์—์„œ ๋‹จ์ˆœํ•˜๊ฒŒ ์ ‘๊ทผ ์ง€๋‚œ ํฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•œ ERD๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Category์™€ Post๊ฐ€ ์žˆ๊ณ , Post๋Š” category_id๋ฅผ ์™ธ๋ž˜ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ category ํ…Œ์ด๋ธ”์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฑด๋ฐ, ์šฐ๋ฆฌ๊ฐ€ ์ด ์ •๋ณด๋ฅผ ๋ชจ๋‘ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ..