{"id":943,"date":"2026-05-26T23:13:38","date_gmt":"2026-05-26T15:13:38","guid":{"rendered":"https:\/\/junai.ai\/blog\/fastapi-crud-8\/"},"modified":"2026-05-26T23:13:38","modified_gmt":"2026-05-26T15:13:38","slug":"fastapi-crud-8","status":"publish","type":"post","link":"https:\/\/junai.ai\/blog\/fastapi-crud-8\/","title":{"rendered":"FastAPI CRUD \u2014 \uc0dd\uc131\u00b7\uc870\ud68c\u00b7\uc218\uc815\u00b7\uc0ad\uc81c"},"content":{"rendered":"\n<!-- WordPress REST API \ubc1c\ud589\uc6a9 HTML (\uc790\ub3d9 \uc0dd\uc131) -->\n<!-- WP-FEATURED-MEDIA-ID: 930 -->\n<div style=\"max-width:800px;margin:0 auto;\">\n<style>\n:root {--color-primary:#00897b;--color-accent:#009688;--color-bg:#fafbfc;--color-bg-card:#fff;--color-text:#1a202c;--color-text-muted:#64748b;--hero-start:#004d40;--hero-end:#00897b;}\n*{box-sizing:border-box;}\n.container{max-width:760px;margin:0 auto;padding:0 24px 80px;}\n.hero{background:linear-gradient(135deg,var(--hero-start) 0%,var(--hero-end) 100%);color:#fff;padding:80px 24px 60px;text-align:center;}\n.hero .eyebrow{display:inline-block;font-size:14px;color:#80cbc4;font-weight:700;letter-spacing:0.1em;text-transform:uppercase;margin-bottom:14px;}\n.hero h1{font-size:36px;margin:0 0 16px;line-height:1.3;font-weight:800;}\n.hero p{color:#b2dfdb;font-size:18px;max-width:640px;margin:0 auto;line-height:1.6;}\n.hero img{width:100%;max-width:640px;height:auto;margin:32px auto 0;border-radius:10px;display:block;}\narticle{padding-top:48px;}\narticle h2{font-size:26px;margin:56px 0 20px;padding-left:14px;border-left:5px solid var(--color-accent);line-height:1.4;}\narticle h3{font-size:19px;margin:32px 0 12px;color:var(--color-primary);}\narticle p{margin:16px 0;}\narticle strong{color:var(--color-primary);font-weight:700;}\narticle code{background:#b2dfdb;padding:2px 8px;border-radius:4px;font-family:'SF Mono',Menlo,Consolas,monospace;font-size:14px;color:#004d40;}\n.databox{background:#b2dfdb;border-left:4px solid var(--color-accent);padding:16px 20px;margin:24px 0;border-radius:0 8px 8px 0;font-size:15.5px;}\n.databox strong{color:var(--color-primary);}\n.warnbox{background:linear-gradient(135deg,#fef3c7 0%,#fde68a 100%);padding:16px 20px;margin:24px 0;border-radius:8px;font-size:15.5px;}\n.tablewrap{overflow-x:auto;-webkit-overflow-scrolling:touch;margin:22px 0;}\ntable{width:100%;border-collapse:collapse;font-size:15px;background:var(--color-bg-card);}\nth,td{padding:11px 12px;text-align:left;border-bottom:1px solid #e2e8f0;vertical-align:top;}\nth{background:#f1f5f9;font-weight:700;color:#0f172a;}\ntd:first-child,th:first-child{font-weight:700;}\n@media (max-width:560px){.tablewrap table,.tablewrap thead,.tablewrap tbody,.tablewrap tr,.tablewrap th,.tablewrap td{display:block;width:auto;}.tablewrap thead{display:none;}.tablewrap tr{margin:0 0 14px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden;}.tablewrap td{border:none;border-bottom:1px solid #f1f5f9;padding:9px 14px;}.tablewrap td:first-child{background:#f1f5f9;font-weight:800;font-size:15.5px;}.tablewrap td:last-child{border-bottom:none;}.tablewrap td[data-label]::before{content:attr(data-label) \" \u2014 \";font-weight:700;color:var(--color-primary);}}\n.code-block{background:#0f172a;color:#e2e8f0;padding:16px 20px;border-radius:8px;font-family:'SF Mono',Menlo,Consolas,monospace;font-size:14px;line-height:1.6;margin:20px 0;overflow-x:auto;white-space:pre;}\n.cta{background:linear-gradient(135deg,#00897b 0%,#009688 100%);color:#fff;padding:28px 24px;border-radius:12px;margin:48px 0 0;text-align:center;}\n.cta h3{color:#fff;margin:0 0 8px;font-size:20px;}\n.cta p{color:#b2dfdb;margin:0;font-size:15.5px;}\n.footer-nav{margin-top:32px;padding-top:20px;border-top:1px solid #e2e8f0;font-size:14px;color:var(--color-text-muted);}\n.footer-nav a{color:var(--color-primary);text-decoration:none;}\n@media (max-width:480px){.hero h1{font-size:26px;}.hero p{font-size:16px;}article h2{font-size:21px;}article h3{font-size:17px;}body{font-size:16px;}}\n<\/style>\n<section class=\"hero\">\n  <span class=\"eyebrow\">FastAPI \uad50\uc7ac \u00b7 8\ud3b8 \u00b7 CRUD<\/span>\n  <h1>FastAPI CRUD \u2014 \uc0dd\uc131\u00b7\uc870\ud68c\u00b7\uc218\uc815\u00b7\uc0ad\uc81c<\/h1>\n  <p>\uc55e \ud3b8\uc5d0\uc11c \ub9cc\ub4e0 User \ubaa8\ub378\uacfc DB \uc138\uc158 \uc704\uc5d0, \ub370\uc774\ud130\ub97c \ub9cc\ub4e4\uace0 \uc77d\uace0 \uace0\uce58\uace0 \uc9c0\uc6b0\ub294 \ub124 \ub3d9\uc791\uc744 \ud55c \ubc88\uc5d0 \uc644\uc131\ud55c\ub2e4.<\/p>\n  <img decoding=\"async\" src=\"https:\/\/junai.ai\/blog\/wp-content\/uploads\/2026\/05\/hero-5-105.jpg\" alt=\"\ub370\uc774\ud130\ubca0\uc774\uc2a4\ub97c \uc911\uc2ec\uc73c\ub85c \uc0dd\uc131\u00b7\uc870\ud68c\u00b7\uc218\uc815\u00b7\uc0ad\uc81c \ub124 \ub3d9\uc791\uc774 \uc21c\ud658\ud558\ub294 FastAPI CRUD \uc544\uc774\uc18c\uba54\ud2b8\ub9ad \uc77c\ub7ec\uc2a4\ud2b8\">\n<\/section>\n\n<div class=\"container\">\n<article>\n\n<p>7\ud3b8\uc5d0\uc11c \uc6b0\ub9ac\ub294 SQLAlchemy \ub85c <code>User<\/code> \ubaa8\ub378\uc744 \uc815\uc758\ud558\uace0, \uc694\uccad\ub9c8\ub2e4 <code>Depends(get_db)<\/code> \ub85c DB \uc138\uc158\uc744 \ubc1b\uc544 \uc4f0\ub294 \uad6c\uc870\uae4c\uc9c0 \ub9cc\ub4e4\uc5c8\ub2e4. \uc774\uc81c \uadf8 \ud1a0\ub300 \uc704\uc5d0 \uc9c4\uc9dc \uc54c\ub9f9\uc774\ub97c \uc62c\ub9b0\ub2e4 \u2014 <strong>CRUD<\/strong>, \uc989 \ub370\uc774\ud130\ub97c <strong>\ub9cc\ub4e4\uace0(Create)\u00b7\uc77d\uace0(Read)\u00b7\uace0\uce58\uace0(Update)\u00b7\uc9c0\uc6b0\ub294(Delete)<\/strong> \ub124 \ub3d9\uc791\uc774\ub2e4.<\/p>\n\n<p>\uac70\uc758 \ubaa8\ub4e0 \ubc31\uc5d4\ub4dc API \ub294 \uacb0\uad6d \uc774 \ub124 \uac00\uc9c0\uc758 \ubc18\ubcf5\uc774\ub2e4. \ud68c\uc6d0 \uac00\uc785\uc740 Create, \ud504\ub85c\ud544 \uc870\ud68c\ub294 Read, \ub2c9\ub124\uc784 \ubcc0\uacbd\uc740 Update, \ud0c8\ud1f4\ub294 Delete. \uc774\ubc88 \ud3b8\uc744 \ub05d\ub0b4\uba74 User \ud55c \ud14c\uc774\ube14\uc5d0 \ub300\ud574 \uc791\ub3d9\ud558\ub294 \uc644\uacb0\ub41c API \ud55c \ubc8c\uc744 \uac16\uac8c \ub41c\ub2e4.<\/p>\n\n<h2>1. CRUD \uac00 \ubb54\uac00 \u2014 \ub124 \ub3d9\uc791\uacfc HTTP \uba54\uc11c\ub4dc<\/h2>\n\n<p>CRUD \ub294 \ub370\uc774\ud130\ub97c \ub2e4\ub8e8\ub294 \ub124 \uac00\uc9c0 \uae30\ubcf8 \ub3d9\uc791\uc758 \uba38\ub9ac\uae00\uc790\ub2e4. \uadf8\ub9ac\uace0 REST API \uc5d0\uc11c\ub294 \uc774 \ub124 \ub3d9\uc791\uc774 \uac01\uac01 \uc815\ud574\uc9c4 <strong>HTTP \uba54\uc11c\ub4dc<\/strong>\uc5d0 \ub300\uc751\ud55c\ub2e4. \uc678\uc6b8 \ud544\uc694 \uc5c6\uc774 \ud45c \ud55c \uc7a5\uc774\uba74 \ud3c9\uc0dd \uc4f4\ub2e4.<\/p>\n\n<div class=\"tablewrap\">\n<table>\n<thead><tr><th>CRUD<\/th><th>\ub73b<\/th><th>HTTP \uba54\uc11c\ub4dc<\/th><th>\uc608\uc2dc \uacbd\ub85c<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>Create<\/td><td data-label=\"\ub73b\">\uc0c8\ub85c \ub9cc\ub4e0\ub2e4<\/td><td data-label=\"HTTP \uba54\uc11c\ub4dc\">POST<\/td><td data-label=\"\uc608\uc2dc \uacbd\ub85c\">POST \/users<\/td><\/tr>\n<tr><td>Read<\/td><td data-label=\"\ub73b\">\uc77d\uc5b4 \uc628\ub2e4<\/td><td data-label=\"HTTP \uba54\uc11c\ub4dc\">GET<\/td><td data-label=\"\uc608\uc2dc \uacbd\ub85c\">GET \/users \u00b7 GET \/users\/1<\/td><\/tr>\n<tr><td>Update<\/td><td data-label=\"\ub73b\">\uace0\uce5c\ub2e4<\/td><td data-label=\"HTTP \uba54\uc11c\ub4dc\">PUT \/ PATCH<\/td><td data-label=\"\uc608\uc2dc \uacbd\ub85c\">PUT \/users\/1<\/td><\/tr>\n<tr><td>Delete<\/td><td data-label=\"\ub73b\">\uc9c0\uc6b4\ub2e4<\/td><td data-label=\"HTTP \uba54\uc11c\ub4dc\">DELETE<\/td><td data-label=\"\uc608\uc2dc \uacbd\ub85c\">DELETE \/users\/1<\/td><\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n\n<p>\uc5ec\uae30 \uae54\ub9b0 \uaddc\uce59\uc740 \uc758\uc678\ub85c \ub2e8\uc21c\ud558\ub2e4 \u2014 <strong>&#8220;\ubb34\uc5c7\uc744 \ud560\uc9c0&#8221; \ub294 \uba54\uc11c\ub4dc\uac00, &#8220;\ubb34\uc5c7\uc5d0 \ud560\uc9c0&#8221; \ub294 \uacbd\ub85c\uac00 \ub9d0\ud55c\ub2e4.<\/strong> \uac19\uc740 <code>\/users\/1<\/code> \uc8fc\uc18c\ub77c\ub3c4 GET \uc73c\ub85c \ubd80\ub974\uba74 \uc870\ud68c, DELETE \ub85c \ubd80\ub974\uba74 \uc0ad\uc81c\ub2e4. \ud589\ub3d9(\ub3d9\uc0ac)\uc744 URL \uc5d0 \ub123\uc9c0 \uc54a\uace0 \uba54\uc11c\ub4dc\ub85c \ubd84\ub9ac\ud558\ub294 \uac83\uc774 REST \uc758 \ud575\uc2ec\uc774\ub2e4.<\/p>\n\n<div class=\"databox\">\n<strong>PUT \uacfc PATCH \uc758 \ucc28\uc774<\/strong> \u2014 <strong>PUT<\/strong> \uc740 \ub9ac\uc18c\uc2a4\ub97c \ud1b5\uc9f8\ub85c \uad50\uccb4(\ubaa8\ub4e0 \ud544\ub4dc\ub97c \uc0c8 \uac12\uc73c\ub85c), <strong>PATCH<\/strong> \ub294 \uc77c\ubd80 \ud544\ub4dc\ub9cc \ubd80\ubd84 \uc218\uc815\ud55c\ub2e4. \uc785\ubb38 \ub2e8\uacc4\uc5d0\uc11c\ub294 PUT \ud558\ub098\ub85c \ucda9\ubd84\ud558\ub2c8 \uc774\ubc88 \ud3b8\uc740 PUT \uc73c\ub85c \uc9c4\ud589\ud558\uace0, \ubd80\ubd84 \uc218\uc815\uc774 \ud544\uc694\ud574\uc9c0\uba74 \uadf8\ub54c PATCH \ub97c \uaebc\ub0b4\uba74 \ub41c\ub2e4.<\/div>\n\n<h2>2. Create &amp; Read \u2014 \ub9cc\ub4e4\uace0 \uc77d\uae30<\/h2>\n\n<p>\uba3c\uc800 \uc804\uc81c\ub97c \ub2e4\uc2dc \uae54\uc544 \ub454\ub2e4. \uc55e \ud3b8\uc5d0\uc11c \ub9cc\ub4e0 \uac83\ub4e4\uc744 \uadf8\ub300\ub85c \uac00\uc838\uc628\ub2e4 \u2014 SQLAlchemy <code>User<\/code> \ubaa8\ub378, \uc138\uc158\uc744 \uc8fc\ub294 <code>get_db<\/code>, \uadf8\ub9ac\uace0 3\ud3b8\uc5d0\uc11c \uc775\ud78c Pydantic \uc2a4\ud0a4\ub9c8\ub2e4. Pydantic v2 \uc5d0\uc11c\ub294 ORM \uac1d\uccb4\ub97c \uadf8\ub300\ub85c \uc751\ub2f5\uc73c\ub85c \ubcc0\ud658\ud558\uae30 \uc704\ud574 <code>from_attributes=True<\/code> (\uad6c\ubc84\uc804\uc758 <code>orm_mode<\/code>) \ub97c \ucf20\ub2e4.<\/p>\n\n<div class=\"code-block\"># schemas.py\nfrom pydantic import BaseModel, EmailStr, ConfigDict\n\nclass UserCreate(BaseModel):       # \uc785\ub825\uc6a9 \u2014 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \ubcf4\ub0b4\ub294 \uac83\n    name: str\n    email: EmailStr\n\nclass UserOut(BaseModel):          # \ucd9c\ub825\uc6a9 \u2014 \uc751\ub2f5\uc73c\ub85c \ub3cc\ub824\uc904 \uac83\n    id: int\n    name: str\n    email: EmailStr\n    model_config = ConfigDict(from_attributes=True)   # ORM \uac1d\uccb4 \u2192 \uc2a4\ud0a4\ub9c8 \uc790\ub3d9 \ubcc0\ud658<\/div>\n\n<p>\uc785\ub825\uacfc \ucd9c\ub825 \uc2a4\ud0a4\ub9c8\ub97c \ub098\ub204\ub294 \uac74 \uc77c\ubd80\ub7ec\ub2e4. \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 <code>id<\/code> \ub97c \uba4b\ub300\ub85c \uc815\ud574 \ubcf4\ub0b4\uba74 \uc548 \ub418\uace0(\uadf8\uac74 DB \uac00 \ubd80\uc5ec\ud55c\ub2e4), \uc751\ub2f5\uc5d0\ub294 \ube44\ubc00\ubc88\ud638 \uac19\uc740 \ud544\ub4dc\ub97c \uc228\uae38 \uc218\ub3c4 \uc788\uc5b4\uc57c \ud558\uae30 \ub54c\ubb38\uc774\ub2e4. \uc774\uc81c Create \uc640 Read \ub77c\uc6b0\ud2b8\ub97c \uc801\ub294\ub2e4.<\/p>\n\n<div class=\"code-block\"># main.py\nfrom fastapi import FastAPI, Depends, HTTPException\nfrom sqlalchemy.orm import Session\nfrom database import get_db\nimport models, schemas\n\napp = FastAPI()\n\n# Create \u2014 \uc0c8 \uc720\uc800 \uc0dd\uc131\n@app.post(&#8220;\/users&#8221;, response_model=schemas.UserOut, status_code=201)\ndef create_user(payload: schemas.UserCreate, db: Session = Depends(get_db)):\n    user = models.User(name=payload.name, email=payload.email)\n    db.add(user)        # \uc138\uc158\uc5d0 \ucd94\uac00\n    db.commit()         # DB \uc5d0 \ud655\uc815\n    db.refresh(user)    # \ubd80\uc5ec\ub41c id \ub4f1\uc744 \uac1d\uccb4\uc5d0 \ub2e4\uc2dc \ucc44\uc6c0\n    return user\n\n# Read \ubaa9\ub85d \u2014 \uc804\uccb4 \uc720\uc800\n@app.get(&#8220;\/users&#8221;, response_model=list[schemas.UserOut])\ndef list_users(db: Session = Depends(get_db)):\n    return db.query(models.User).all()\n\n# Read \ub2e8\uac74 \u2014 id \ub85c \ud55c \uba85, \uc5c6\uc73c\uba74 404\n@app.get(&#8220;\/users\/{user_id}&#8221;, response_model=schemas.UserOut)\ndef get_user(user_id: int, db: Session = Depends(get_db)):\n    user = db.get(models.User, user_id)\n    if user is None:\n        raise HTTPException(status_code=404, detail=&#8221;\uc720\uc800\ub97c \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4&#8221;)\n    return user<\/div>\n\n<p>\uc138 \uc904\uc774 \ud575\uc2ec\uc774\ub2e4 \u2014 <code>db.add()<\/code> \ub85c \uc138\uc158\uc5d0 \uc62c\ub9ac\uace0, <code>db.commit()<\/code> \uc73c\ub85c DB \uc5d0 \ud655\uc815\ud558\uace0, <code>db.refresh()<\/code> \ub85c DB \uac00 \ubd80\uc5ec\ud55c <code>id<\/code> \ub97c \uac1d\uccb4\uc5d0 \ub418\ucc44\uc6b4\ub2e4. \uc0dd\uc131 \uc751\ub2f5\uc5d0\ub294 <code>status_code=201<\/code>(Created)\uc744 \ubd99\uc5ec \uc758\ubbf8\ub97c \uba85\ud655\ud788 \ud588\ub2e4. \ub2e8\uac74 \uc870\ud68c\uc5d0\uc11c \ub370\uc774\ud130\uac00 \uc5c6\uc73c\uba74 <code>HTTPException(status_code=404)<\/code> \ub97c \ub358\uc9c4\ub2e4 \u2014 4\ud3b8\uc5d0\uc11c \ubc30\uc6b4 \uadf8 \ubc29\uc2dd \uadf8\ub300\ub85c\ub2e4. \ube48 \uc751\ub2f5\uc744 200 \uc73c\ub85c \ub3cc\ub824\uc8fc\ub294 \uac83\ubcf4\ub2e4 \uc815\uc9c1\ud558\ub2e4.<\/p>\n\n<h2>3. Update &amp; Delete \u2014 \uace0\uce58\uace0 \uc9c0\uc6b0\uae30<\/h2>\n\n<p>\ub098\uba38\uc9c0 \ub450 \ub3d9\uc791\ub3c4 \ud328\ud134\uc740 \uac19\ub2e4. <strong>\uba3c\uc800 \ub300\uc0c1\uc744 \ucc3e\uace0(\uc5c6\uc73c\uba74 404), \uadf8\ub2e4\uc74c \uc190\uc744 \ub304\ub2e4.<\/strong> Update \ub294 \ucc3e\uc740 \uac1d\uccb4\uc758 \ud544\ub4dc\ub97c \uc0c8 \uac12\uc73c\ub85c \ubc14\uafbc \ub4a4 commit, Delete \ub294 <code>db.delete()<\/code> \ud6c4 commit \uc774\ub2e4.<\/p>\n\n<div class=\"code-block\"># Update \u2014 \uae30\uc874 \uc720\uc800 \uc218\uc815 (PUT, \uc804\uccb4 \uad50\uccb4)\n@app.put(&#8220;\/users\/{user_id}&#8221;, response_model=schemas.UserOut)\ndef update_user(user_id: int, payload: schemas.UserCreate,\n                db: Session = Depends(get_db)):\n    user = db.get(models.User, user_id)\n    if user is None:\n        raise HTTPException(status_code=404, detail=&#8221;\uc720\uc800\ub97c \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4&#8221;)\n    user.name = payload.name      # \ucc3e\uc740 \uac1d\uccb4\uc758 \ud544\ub4dc\ub97c \ubc14\uafb8\uace0\n    user.email = payload.email\n    db.commit()                   # commit \ud558\uba74 \uadf8\ub300\ub85c UPDATE \uac00 \ub098\uac04\ub2e4\n    db.refresh(user)\n    return user\n\n# Delete \u2014 \uc720\uc800 \uc0ad\uc81c, \ubcf8\ubb38 \uc5c6\uc774 204\n@app.delete(&#8220;\/users\/{user_id}&#8221;, status_code=204)\ndef delete_user(user_id: int, db: Session = Depends(get_db)):\n    user = db.get(models.User, user_id)\n    if user is None:\n        raise HTTPException(status_code=404, detail=&#8221;\uc720\uc800\ub97c \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4&#8221;)\n    db.delete(user)\n    db.commit()\n    return None       # 204 No Content \u2014 \ub3cc\ub824\uc904 \ubcf8\ubb38\uc774 \uc5c6\ub2e4<\/div>\n\n<p>Update \uc5d0\uc11c \uc2e0\uae30\ud55c \uc810 \ud558\ub098 \u2014 \uc6b0\ub9ac\ub294 SQL \uc758 <code>UPDATE<\/code> \ubb38\uc744 \ud55c \uc904\ub3c4 \uc548 \uc801\uc5c8\ub2e4. SQLAlchemy \uc138\uc158\uc774 \uac1d\uccb4\uc758 \ubcc0\uacbd\uc744 \ucd94\uc801\ud558\uace0 \uc788\ub2e4\uac00, <code>commit()<\/code> \uc21c\uac04 \ubc14\ub010 \ud544\ub4dc\ub97c \uc54c\uc544\uc11c SQL \ub85c \ub9cc\ub4e4\uc5b4 \ubcf4\ub0b8\ub2e4. \uc774\uac78 <strong>\ubcc0\uacbd \ucd94\uc801(dirty tracking)<\/strong> \uc774\ub77c \ubd80\ub978\ub2e4. Delete \uc751\ub2f5\uc740 <code>status_code=204<\/code>(No Content) \u2014 \uc0ad\uc81c \ud6c4\uc5d0\ub294 \ub3cc\ub824\uc904 \ub370\uc774\ud130\uac00 \uc5c6\uc73c\ub2c8 \ubcf8\ubb38 \uc5c6\uc774 &#8220;\ucc98\ub9ac\ub410\ub2e4&#8221; \ub9cc \uc54c\ub9b0\ub2e4.<\/p>\n\n<div class=\"warnbox\">\n<strong>commit \uc744 \ube60\ub728\ub9ac\uba74?<\/strong> \u2014 <code>db.add()<\/code>\u00b7<code>db.delete()<\/code> \ub098 \ud544\ub4dc \ubcc0\uacbd\uae4c\uc9c0\ub9cc \ud558\uace0 <code>commit()<\/code> \uc744 \uc548 \ud558\uba74, \uc138\uc158\uc774 \ub05d\ub0a0 \ub54c \ubcc0\uacbd\uc774 \ud1b5\uc9f8\ub85c <strong>\ub864\ubc31<\/strong>\ub3fc DB \uc5d0\ub294 \uc544\ubb34 \uc77c\ub3c4 \uc548 \uc77c\uc5b4\ub09c\ub2e4. &#8220;\ucf54\ub4dc\ub294 \ub9de\ub294\ub370 DB \uc5d0 \uc548 \ub4e4\uc5b4\uac00\uc694&#8221; \uc758 90% \ub294 \ube60\uc9c4 commit \uc774\ub2e4. \uc4f0\uae30 \ub3d9\uc791 \ub4a4\uc5d4 commit \uc744 \uc2b5\uad00\uc73c\ub85c.\n<\/div>\n\n<h2>4. crud.py \ub85c \ubd84\ub9ac \u2014 \ub77c\uc6b0\ud2b8\ub294 \uc587\uac8c<\/h2>\n\n<p>\uc704 \ucf54\ub4dc\ub97c \ubcf4\uba74 <code>db.get(...)<\/code>, 404 \uccb4\ud06c, <code>db.add\/commit<\/code> \uac19\uc740 DB \ub85c\uc9c1\uc774 \ub77c\uc6b0\ud2b8 \ud568\uc218\ub9c8\ub2e4 \ubc18\ubcf5\ub41c\ub2e4. \ub77c\uc6b0\ud2b8\uac00 \ucee4\uc9c0\uba74 &#8220;HTTP \ucc98\ub9ac&#8221; \uc640 &#8220;DB \ucc98\ub9ac&#8221; \uac00 \ub4a4\uc5c9\ucf1c \uc77d\uae30\ub3c4, \ud14c\uc2a4\ud2b8\ud558\uae30\ub3c4 \uc5b4\ub824\uc6cc\uc9c4\ub2e4. \uadf8\ub798\uc11c \uc2e4\ubb34\uc5d0\uc11c\ub294 <strong>DB \ub85c\uc9c1\uc744 <code>crud.py<\/code> \uc758 \ud3c9\ubc94\ud55c \ud568\uc218\ub85c \ube7c\ub0b4\uace0, \ub77c\uc6b0\ud2b8\ub294 \uadf8 \ud568\uc218\ub97c \ubd80\ub974\uae30\ub9cc \ud558\uac8c<\/strong> \ub9cc\ub4e0\ub2e4.<\/p>\n\n<div class=\"code-block\"># crud.py \u2014 DB \ub85c\uc9c1\ub9cc \ubaa8\uc740\ub2e4 (HTTP \ub97c \ubaa8\ub984)\nfrom sqlalchemy.orm import Session\nimport models, schemas\n\ndef get_user(db: Session, user_id: int):\n    return db.get(models.User, user_id)\n\ndef create_user(db: Session, payload: schemas.UserCreate):\n    user = models.User(name=payload.name, email=payload.email)\n    db.add(user); db.commit(); db.refresh(user)\n    return user<\/div>\n\n<p>\uc774\uc81c \ub77c\uc6b0\ud2b8\ub294 \uc587\uc544\uc9c4\ub2e4. HTTP \uc758 \uc77c(404 \ub97c \ub358\uc9c8\uc9c0 \ub9d0\uc9c0)\ub9cc \ub0a8\uace0, DB \uc758 \uc77c\uc740 crud \ud568\uc218\uc5d0 \uc704\uc784\ud55c\ub2e4.<\/p>\n\n<div class=\"code-block\"># main.py \u2014 \ub77c\uc6b0\ud2b8\ub294 crud \ud568\uc218\ub97c \ubd80\ub974\uae30\ub9cc\n@app.post(&#8220;\/users&#8221;, response_model=schemas.UserOut, status_code=201)\ndef create_user(payload: schemas.UserCreate, db: Session = Depends(get_db)):\n    return crud.create_user(db, payload)\n\n@app.get(&#8220;\/users\/{user_id}&#8221;, response_model=schemas.UserOut)\ndef read_user(user_id: int, db: Session = Depends(get_db)):\n    user = crud.get_user(db, user_id)\n    if user is None:\n        raise HTTPException(status_code=404, detail=&#8221;\uc720\uc800\ub97c \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4&#8221;)\n    return user<\/div>\n\n<p>\uc774\ub807\uac8c \ub098\ub204\uba74 \uac19\uc740 <code>crud.get_user()<\/code> \ub97c \ub2e4\ub978 \ub77c\uc6b0\ud2b8\uc5d0\uc11c\ub3c4 \uc7ac\uc0ac\uc6a9\ud560 \uc218 \uc788\uace0, \ud14c\uc2a4\ud2b8\ud560 \ub54c HTTP \uc11c\ubc84\ub97c \ub744\uc6b8 \ud544\uc694 \uc5c6\uc774 \ud568\uc218\uc5d0 \uc138\uc158\ub9cc \ub118\uaca8 \uc9c1\uc811 \uac80\uc99d\ud560 \uc218 \uc788\ub2e4. 6\ud3b8\uc5d0\uc11c \ub2e4\ub8ec \ub77c\uc6b0\ud130 \ubd84\ub9ac\uc640 \uac19\uc740 \uc815\uc2e0\uc774\ub2e4 \u2014 <strong>\uac01 \ud30c\uc77c\uc774 \ud55c \uac00\uc9c0 \uc77c\ub9cc \ud558\uac8c<\/strong>.<\/p>\n\n<h3>\uc694\uc57d<\/h3>\n\n<p>\uc774\ubc88 \ud3b8\uc5d0\uc11c CRUD \ud55c \ubc8c\uc744 \uc644\uc131\ud588\ub2e4. <strong>POST \ub85c \ub9cc\ub4e4\uace0(add\u00b7commit\u00b7refresh), GET \uc73c\ub85c \uc77d\uace0(\ubaa9\ub85d\uc740 <code>.all()<\/code>, \ub2e8\uac74\uc740 \uc5c6\uc73c\uba74 404), PUT \uc73c\ub85c \uace0\uce58\uace0(\ud544\ub4dc \ubcc0\uacbd \ud6c4 commit), DELETE \ub85c \uc9c0\uc6b4\ub2e4(delete\u00b7commit, 204).<\/strong> \ubaa8\ub4e0 \uc4f0\uae30 \ub3d9\uc791\uc758 \ub05d\uc5d0\ub294 <code>commit()<\/code> \uc774 \uc788\uace0, \ub300\uc0c1\uc774 \uc5c6\uc744 \ub550 \uc815\uc9c1\ud558\uac8c <code>HTTPException(404)<\/code> \ub97c \ub358\uc9c4\ub2e4. \uadf8\ub9ac\uace0 DB \ub85c\uc9c1\uc740 <code>crud.py<\/code> \ub85c \ube7c\uc11c \ub77c\uc6b0\ud2b8\ub97c \uc587\uac8c \uc720\uc9c0\ud588\ub2e4. \uc774\uc81c User \ud14c\uc774\ube14 \ud558\ub098\uc5d0 \ub300\ud55c API \uac00 \uc644\uacb0\ub410\ub2e4.<\/p>\n\n<p>\ud558\uc9c0\ub9cc \ube60\uc9c4 \uac8c \ud558\ub098 \uc788\ub2e4 \u2014 \uc9c0\uae08\uc740 <strong>\uc544\ubb34\ub098<\/strong> \ub0a8\uc758 \uc720\uc800\ub97c \uc218\uc815\ud558\uace0 \uc0ad\uc81c\ud560 \uc218 \uc788\ub2e4. \ub2e4\uc74c \ud3b8\uc5d0\uc11c \uc774 \ubb38\uc744 \uc7a0\uadfc\ub2e4.<\/p>\n\n<div class=\"cta\">\n<h3>\ub2e4\uc74c \ud3b8 \uc608\uace0 \u2014 9\ud3b8: \uc778\uc99d, OAuth2 \uc640 JWT<\/h3>\n<p>\ub85c\uadf8\uc778\ud55c \uc0ac\ub78c\ub9cc \uc790\uae30 \ub370\uc774\ud130\ub97c \ub2e4\ub8f0 \uc218 \uc788\uac8c. OAuth2 \ube44\ubc00\ubc88\ud638 \ud750\ub984\uacfc JWT \ud1a0\ud070\uc73c\ub85c API \uc5d0 \uc790\ubb3c\uc1e0\ub97c \ub2e8\ub2e4.<\/p>\n<\/div>\n\n<div class=\"footer-nav\">\n\uc2dc\ub9ac\uc988 \u00b7 <a href=\"https:\/\/junai.ai\/blog\/category\/fastapi\/\">\uc27d\uac8c \ubc30\uc6b0\ub294 FastAPI<\/a> \u00b7 \ud568\uaed8 \ubcf4\uae30: <a href=\"https:\/\/junai.ai\/blog\/category\/python\/\">\ud30c\uc774\uc36c \uae30\ucd08 \uc2dc\ub9ac\uc988<\/a>\n<\/div>\n\n<\/article>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>User \ubaa8\ub378\uacfc SQLAlchemy \uc138\uc158 \uc704\uc5d0\uc11c POST\u00b7GET\u00b7PUT\u00b7DELETE \ub85c CRUD \ub97c \uc644\uc131\ud55c\ub2e4. 404 \ucc98\ub9ac\uc640 crud.py \ubd84\ub9ac\uae4c\uc9c0. \uad50\uc7ac 8\ud3b8.<\/p>\n","protected":false},"author":1,"featured_media":930,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fastapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/comments?post=943"}],"version-history":[{"count":0,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media?parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/categories?post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/tags?post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}