1. sqlalchemy CRUD 예제 실습
2. callcenter 프로젝트 제작중
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
||||
from pathlib import Path
|
||||
|
||||
Path("db").mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# echo sql 구문 출력시키는
|
||||
engine = create_engine('sqlite:///db/callcenter.db', echo=True)
|
||||
|
||||
# 세션 팩토리 생성
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
# Base = declarative_Base()
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
# session 을 다른 모듈에서 사용할 수 있도록 제공
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -0,0 +1,30 @@
|
||||
from backend.repository.db_init import Base
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy import DateTime, func, String, ForeignKey
|
||||
from datetime import datetime
|
||||
|
||||
class Customer(Base):
|
||||
__tablename__ = 'customers'
|
||||
|
||||
customer_id:Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
name:Mapped[str] = mapped_column(String(50), nullable=False)
|
||||
phone:Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
|
||||
def __str__(self):
|
||||
return f"<Customer id={self.customer_id} name={self.name} phone = {self.phone}]"
|
||||
|
||||
|
||||
# 상담기록 저장
|
||||
class CallHistory(Base):
|
||||
__tablename__ = 'call_history'
|
||||
|
||||
call_id:Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
customer_id:Mapped[int] = mapped_column(ForeignKey('customers.customer_id'), nullable=False)
|
||||
transcript:Mapped[str]
|
||||
summary:Mapped[str]
|
||||
category:Mapped[str] = mapped_column(String(50))
|
||||
sentiment:Mapped[str] = mapped_column(String(20))
|
||||
customer_issue:Mapped[str]
|
||||
resolution:Mapped[str]
|
||||
# created_at:Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|
||||
created_at:Mapped[datetime] = mapped_column(default=datetime.now, nullable=False)
|
||||
@@ -0,0 +1,28 @@
|
||||
# 강제로 회원가입
|
||||
from sqlalchemy.orm import Session
|
||||
from backend.repository.db_init import SessionLocal
|
||||
from backend.repository.models import Customer
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
DEFAULT_CUSTOMERS = [
|
||||
Customer(name="홍길동", phone="010-1234-5678"),
|
||||
Customer(name="최철수", phone="010-4321-8765"),
|
||||
Customer(name="박영희", phone="010-5678-1234"),
|
||||
]
|
||||
|
||||
def seed_customers(db: Session):
|
||||
"""
|
||||
customer 테이블에 기본(연습용) 회원 데이터 삽입
|
||||
(중복 실행 방지 : 이미 데이터가 있으면 건너뜀)
|
||||
"""
|
||||
|
||||
existing = db.query(Customer).first()
|
||||
if existing:
|
||||
print("[Seed] customer 테이블에 이미 데이터가 있습니다.")
|
||||
return
|
||||
|
||||
db.add_all(DEFAULT_CUSTOMERS)
|
||||
db.commit()
|
||||
db.close()
|
||||
print(f"[Seed] 기본 회원 {len(DEFAULT_CUSTOMERS)}명 생성 완료")
|
||||
Reference in New Issue
Block a user