SQLite 설치 - Download 링크
SQLite 실행
자신이 DB를 만들고자 하는 폴더로 감.
터미널 창에서 명령어 실행
db 파일로 생성하기 위해서 .db로 database를 생성함 : CLI 환경
sqlite3 {원하는 DB명}.db
기본적인 코드
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "sqlite:///app/models/dsdb.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
DB Table 연결 코드
from sqlalchemy import Column, Integer, ForeignKey, BLOB, Text, String
from sqlalchemy.orm import relationship
from app.models.database import Base
class PersonalProfile(Base):
__tablename__ = 'personal_profile'
profile_id = Column(Integer, primary_key=True, unique=True, nullable=False)
name = Column(String, nullable=False)
email = Column(String, nullable=False)
password = Column(String, nullable=False)
phone_number = Column(String, nullable=False)
profile_image = Column(BLOB)
teams_managed = relationship('TeamProfile', back_populates='manager')
issues_published = relationship('Issue', back_populates='publisher')
comments_published = relationship(
'IssueComment', back_populates='publisher')
memberships = relationship('TeamMembership', back_populates='member')
class TeamProfile(Base):
__tablename__ = 'team_profile'
profile_id = Column(Integer, primary_key=True, unique=True, nullable=False)
team_name = Column(String, nullable=False)
team_introduction = Column(Text, nullable=False)
team_manager = Column(Integer, ForeignKey(
'personal_profile.profile_id', onupdate="CASCADE", ondelete="RESTRICT"), nullable=False)
profile_image = Column(BLOB)
manager = relationship('PersonalProfile', back_populates='teams_managed')
memberships = relationship('TeamMembership', back_populates='team')