Develop/FastAPI
FastAPI 데이터베이스 내 User와 Todo의 관계 설정 - 52
김잠봉
2023. 1. 10. 00:24
728x90
데이터베이스 내에서 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