-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentity.py
More file actions
34 lines (26 loc) · 1.23 KB
/
entity.py
File metadata and controls
34 lines (26 loc) · 1.23 KB
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
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from dotenv import load_dotenv
import os
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
engine = create_async_engine(DATABASE_URL, echo=True)
AsyncSessionLocal = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
Base = declarative_base()
# 비동기적으로 테이블을 생성하는 함수
async def create_tables():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
class ChatGroup(Base):
__tablename__ = "chatgroup"
group_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
chats = relationship("Chat", back_populates="group", cascade="all, delete-orphan")
class Chat(Base):
__tablename__ = "chat"
chat_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
group_id = Column(Integer, ForeignKey("chatgroup.group_id"), primary_key=True, index=True)
question = Column(String(2048))
answer = Column(String(2048))
group = relationship("ChatGroup", back_populates="chats")