From efdf48244f305eb9303fe6ec6928e386286b491b Mon Sep 17 00:00:00 2001 From: mjkweon17 Date: Sat, 11 Nov 2023 02:15:12 +0900 Subject: [PATCH] chore: fix typo in create_tables.sql --- app/main.py | 4 ++-- app/models.py | 36 ++++++++++++++++++++++++------------ app/routers/test.py | 7 ++----- create_tables.sql | 6 +++++- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/app/main.py b/app/main.py index 149f9db..8619d8b 100644 --- a/app/main.py +++ b/app/main.py @@ -3,12 +3,12 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -#from routers import auth +from routers import test app = FastAPI() # 라우터 등록 -# app.include_router(auth.router) +app.include_router(test.router) # CORS 설정 origins = [ diff --git a/app/models.py b/app/models.py index 4de9f3d..534074f 100644 --- a/app/models.py +++ b/app/models.py @@ -11,12 +11,19 @@ class HKUser(Base): created_at = Column(DateTime, nullable=False) github_id = Column(String(255), nullable=False) blog_link = Column(String(255), nullable=False) + + reviews = relationship('HKReview', back_populates='user') + bookmarks = relationship('HKBookmark', back_populates='user') + boards = relationship('HKBoard', back_populates='user') + user_stacks = relationship('HKUserStack', back_populates='user') class HKCompany(Base): __tablename__ = 'HKCompany' company_id = Column(Integer, primary_key=True, autoincrement=True) company_name = Column(String(255), nullable=False) + lectures = relationship('HKLecture', back_populates='company') + class HKLecture(Base): __tablename__ = 'HKLecture' lecture_id = Column(Integer, primary_key=True, autoincrement=True) @@ -30,9 +37,15 @@ class HKLecture(Base): difficulty = Column(String(255), nullable=True) company_id = Column(Integer, ForeignKey('HKCompany.company_id'), nullable=False) price = Column(String(255), nullable=True) - discount_rate = Column(String(255), nullable=True) + discount_price = Column(String(255), nullable=True) introduction = Column(Text, nullable=True) + keyword = Column(String(255), nullable=True) + link = Column(String(255), nullable=True) + company = relationship('HKCompany', back_populates='lectures') + lecture_stacks = relationship('HKLectureStack', back_populates='lecture') + reviews = relationship('HKReview', back_populates='lecture') + bookmarks = relationship('HKBookmark', back_populates='lecture') class HKReview(Base): __tablename__ = 'HKReview' @@ -44,6 +57,7 @@ class HKReview(Base): bad_review = Column(Text, nullable=False) is_active = Column(Boolean, nullable=False, default=True) created_at = Column(DateTime, nullable=False) + lecture = relationship('HKLecture', back_populates='reviews') user = relationship('HKUser', back_populates='reviews') @@ -52,6 +66,7 @@ class HKBookmark(Base): bookmark_id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey('HKUser.user_id'), nullable=False) lecture_id = Column(Integer, ForeignKey('HKLecture.lecture_id'), nullable=False) + user = relationship('HKUser', back_populates='bookmarks') lecture = relationship('HKLecture', back_populates='bookmarks') @@ -59,14 +74,18 @@ class HKStackCategory(Base): __tablename__ = 'HKStackCategory' stack_category_id = Column(Integer, primary_key=True, autoincrement=True) parent_id = Column(Integer, ForeignKey('HKStackCategory.stack_category_id'), nullable=True) - stack_name = Column(Integer, nullable=False) + stack_name = Column(String(255), nullable=False) + parent = relationship('HKStackCategory', remote_side=[stack_category_id]) + lecture_stacks = relationship('HKLectureStack', back_populates='stack_category') + user_stacks = relationship('HKUserStack', back_populates='stack_category') class HKLectureStack(Base): __tablename__ = 'HKLectureStack' lecture_stack_id = Column(Integer, primary_key=True, autoincrement=True) stack_category_id = Column(Integer, ForeignKey('HKStackCategory.stack_category_id'), nullable=False) lecture_id = Column(Integer, ForeignKey('HKLecture.lecture_id'), nullable=False) + stack_category = relationship('HKStackCategory', back_populates='lecture_stacks') lecture = relationship('HKLecture', back_populates='lecture_stacks') @@ -75,6 +94,7 @@ class HKUserStack(Base): user_stack_id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey('HKUser.user_id'), nullable=False) stack_category_id = Column(Integer, ForeignKey('HKStackCategory.stack_category_id'), nullable=False) + user = relationship('HKUser', back_populates='user_stacks') stack_category = relationship('HKStackCategory', back_populates='user_stacks') @@ -86,13 +106,5 @@ class HKBoard(Base): created_at = Column(DateTime, nullable=False) is_active = Column(Boolean, nullable=False, default=True) content = Column(Text, nullable=False) - user = relationship('HKUser', back_populates='boards') - -# Set up relationships -HKCompany.lectures = relationship('HKLecture', back_populates='company') -HKLecture.reviews = relationship('HKReview', back_populates='lecture') -HKUser.reviews = relationship('HKReview', back_populates='user') -HKUser.bookmarks = relationship('HKBookmark', back_populates='user') -HKStackCategory.lecture_stacks = relationship('HKLectureStack', back_populates='stack_category') -HKStackCategory.user_stacks = relationship('HKUserStack', back_populates='stack_category') -HKUser.boards = relationship('HKBoard', back_populates='user') + + user = relationship('HKUser', back_populates='boards') \ No newline at end of file diff --git a/app/routers/test.py b/app/routers/test.py index 4f3651c..cc8c631 100644 --- a/app/routers/test.py +++ b/app/routers/test.py @@ -4,13 +4,10 @@ from database import get_db from models import HKUser -router = APIRouter() - -router = APIRouter(previx="/test", tags=["test"], responses={404: {"description": "Not found"}}) +router = APIRouter(prefix="/test", tags=["test"], responses={404: {"description": "Not found"}}) # test 핸들러 @router.get("/") async def get_users(db: Session = Depends(get_db)): users = db.query(HKUser).all() - # return users - return {"message": "users"} \ No newline at end of file + return users \ No newline at end of file diff --git a/create_tables.sql b/create_tables.sql index 0a2926b..9e30214 100644 --- a/create_tables.sql +++ b/create_tables.sql @@ -1,5 +1,7 @@ CREATE TABLE `HKUser` ( `user_id` INT AUTO_INCREMENT NOT NULL, + `user_name` VARCHAR(255) NOT NULL, + `email` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `is_active` BOOL NOT NULL DEFAULT TRUE, `created_at` DATETIME NOT NULL, @@ -26,8 +28,10 @@ CREATE TABLE `HKLecture` ( `difficulty` VARCHAR(255) NULL, `company_id` INT NOT NULL, `price` VARCHAR(255) NULL, - `discount_rate` VARCHAR(255) NULL, + `discount_price` VARCHAR(255) NULL, `introduction` TEXT NULL, + `keyword` VARCHAR(255) NULL, + `link` VARCHAR(255) NULL, PRIMARY KEY (`lecture_id`), FOREIGN KEY (`company_id`) REFERENCES `HKCompany` (`company_id`) ON UPDATE CASCADE );