FastAPI 데이터베이스 내 User와 Todo의 관계 설정 - 52

2023. 1. 10. 00:24·Develop/FastAPI
728x90
SMALL
데이터베이스 내에서 Users와 Todos테이블간의 데이터끼리 관계 설정 해보기[사용자(User) - 할 일(Todo)]

 

1. auth.py 파일 실행 후 테스트 유저 생성

uvicorn auth:app --reload

 

2. 서버 실행을 닫고 터미널에서 생성 확인

※ POST API로 만든 테스트 유저가 id:2로 생성되어있다.

 

3. todos테이블에 데이터 insert 후 select 확인

insert into todos (title, description, priority, complete, owner_id) values ("Take out the dog", "he needs to use the bathroom", 5, false, 1);
insert into todos (title, description, priority, complete, owner_id) values ("Cut the grass", "it is get ting long", 5, false, 1);
insert into todos (title, description, priority, complete, owner_id) values ("Make dinner", "kids are home", 5, false, 2);

 

4. 작업 정리(43페이지 테이블 관계도 참고)

사전에 modles.py에서 생성한 컬럼들을 토대로 데이터를 삽입해줌으로써 Users.id와 Todos.owner_id와의 관계를 생성했다.

즉, Foreign Key를 생성했다.

Table Column Value Table Column Value
users id 1 todos owner_id 1
users id 2 todos owner_id 1
      todos owner_id 2
users.id = 1 & todos.owner_id = 1 Foreign key 생성
users.id = 2 & todos.owner_id = 2 Foreign key 생성
728x90
SMALL
저작자표시 비영리 변경금지 (새창열림)

'Develop > FastAPI' 카테고리의 다른 글

FastAPI Postman을 통한 GET요청(사용자 ID + 할 일 ID) - 54  (0) 2023.01.10
FastAPI Postman을 통한 GET요청(사용자 ID) - 53  (0) 2023.01.10
FastAPI Postman 설치 / 사용법(GET kakao 검색) - 51  (2) 2023.01.09
FastAPI Custom HTTPException for Auth - 50  (0) 2023.01.08
FastAPI JSON Web Token(JWT) 디코딩 함수 구현 - 49  (0) 2023.01.08
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI Postman을 통한 GET요청(사용자 ID + 할 일 ID) - 54
  • FastAPI Postman을 통한 GET요청(사용자 ID) - 53
  • FastAPI Postman 설치 / 사용법(GET kakao 검색) - 51
  • FastAPI Custom HTTPException for Auth - 50
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI 데이터베이스 내 User와 Todo의 관계 설정 - 52
상단으로

티스토리툴바