웹개발일지

[1-2] TablePlus - ERROR: table " " does not exist 본문

데이터베이스

[1-2] TablePlus - ERROR: table " " does not exist

hee_log 2023. 1. 11. 18:53
728x90

 

이 글은 아래 강좌를 듣고 학습하며 겪은 시행착오를 정리한 글입니다. 

 

Backend Master Class [Golang + Postgres + Kubernetes + gRPC]

 

Backend Master Class [Golang + Postgres + Kubernetes + gRPC]

Learn everything about backend web development: Golang, Postgres, Redis, Gin, gRPC, Docker, Kubernetes, AWS, CI/CD

www.udemy.com

https://tableplus.com/blog/2019/09/ddl-statements-sql.html

 

 

DDL statements in SQL

The meaning of DDL

tableplus.com

 

 프로젝트로 진행할 Simple bank 데이터베이스를 을 불러와야하는데 기존 스키마 설계 작업에서 오타를 내는 바람에 제대로된 데이터베이스를 불러오지 못하고 기존 diagram에 있던 샘플 데이터베이스를 불러오는 바람에 실습을 할 수가 없어 테이블을 모두 삭제하고자 했다. 

 

그런데..! 테이블을 삭제하는 작업이 여간 쉽지가 않았다. GUI 로 그냥 우클릭해서 전체 테이블을 드래그하여 delete를 시도했으나 CASCADE 여부 체크부터 한 번에 삭제되지 않는 테이블들이 엮인 시스템을 이해할 수 없었다.. 삭제를 한 후 파일을 저장하래서 저장하고 나가려고 했으나 아래와 같은 오류가 발생하였다. 

 

 

ERROR:  table "entries" does not exist

All changes were reverted (DDL statements can't be reverted).

 

 구글링으로 데이터베이스를 삭제하는 법을 찾으면 꽤나 간단했다. 그냥 DROP DATABASE 에 데이터베이스이름을 넣어주면됐다. 하지만 데이터베이스 이름도 계속 존재하지 않다고 하여 진행할 수가 없었다. 그래서 디스코드에 아래와 같이 질문을 남기고 받은 답변대로 진행을 해보는데 .. 

 

더보기
DROP TABLE IF EXISTS "your table name" CASCADE;
 
 
A:  the easy way is just to use this query for all drop query you write: DROP TABLE IF EXISTS "your table name" CASCADE;

 

Q: What it means? After I drop each tables and I tried to save and out, it occures. "ERROR: table "entries" does not exist. All changes were reverted (DDL statements can't be reverted)." (수정됨)

 

 

A: did you try to delete it manually in tableplus?
  1. 오후 6:00
  2. since doing so might cause a problem because of foreign keys
  3. 오후 6:04
    one way to do it properly is executing this command for every table u have (the order of the call doesn't matter)

     

    DROP TABLE IF EXISTS "your table name" CASCADE;
    from the sql query tool inside the tableplus another way to do it properly is to manually delete the table from tableplus but in the correct order (not recommended). i.e. You have 3 tables: user, role, item user has roleId --> user depends on table role item has userId --> item depends on table user role doens't have any foreign key --> role is independent In order to delete these 3 tables manually from tableplus, you need to delete them in exactly this order because of the relationship of these tables:
    1. delete item 2. delete user 3. delete role

    since doing so might cause a problem because of foreign keys

     

    if i recall correctly, if you delete manually all tables inside tableplus AND ONLY THEN you saved it (ctrl+s), it will try to delete the tables in the alphabet order, hence it's prone to error due to foreign keys conflict

     
    The error "table entries does not exist" means that you're referencing that "entries" table somewhere along your code after you have deleted it, try sending your code here or double checking whether you're referencing it somewhere (수정됨)
    The error "table entries does not exist" means that you're referencing that "entries" table somewhere along your code after you have deleted it, try sending your code here or double checking whether you're referencing it somewhere (수정됨)

     

     
    If I had made tables as same name in many times, Do I have to drop several?

     

    i'm pretty sure it's not possible to create multiple tables with the same name in the same database and connection
     
    you will get an error that a table with the same name already exists if you try to do that

 

 테이블을 하나씩 삭제하고 난 뒤에도 오류가 발생하여 따져본 결과 삭제된 테이블을 다른 테이블에서 참조하고 있어 그렇다는 내용이었다. 지난 수업 때 살짝 개념으로 익혔던 Foreign Key가 그 역할을 한다. 이 왜래키라는 녀석은 테이블간 서로 연결을 해주는 역할을 한다. 이를 부모-자식 관계라고 하는데 이렇게 테이블이 엮여있으면 자식 테이블들 먼저 삭제해줘야 한다는 것이다. 

 

 따라서 accounts 를 삭제하기 전에 entries와 transfers를 먼저 삭제해줘야했던 것이다. 다시 테이블을 가져와 참고해보면 transfers와 entries 의  외래키들이 accounts 테이블의 기록을 참조하고있어 accounts 를 먼저 지울 수가 없는 것이 그 이유이다. 

 

Foreign Key

 외래키라고한다. 테이블간 서로 연결 해주는 역할을 하며, 왜래키가 적용된 테이블은 drop table 명령으로 테이블 삭제시 순서를 유의해야한다. 

 

CASCADE 

 왜래키 옵션이다. 부모 데이터 삭제시 자식데이터도 삭제하도록 한다. 

 

 전체 db를 삭제하는 방법

 모든 테이블을 삭제해도 괜찮다면 좀 더 빠르게 할 수 있는 방법도 있다고 한다. 전체 db를 날리고 다시 만드는 건데 이렇게 하면 된다. 

docker exec -it postgres dropdb simple_bank
docker exec -it postgres createdb --username=root --owner=root simple_bank
make dropdb
make createdb

 

 새로 불러온 테이블의 모습 

 Simple Bank 데이터베이스를 다시 불러온 후 run 한 뒤 테이블 structure를 확인해보니 각 Column, type, foreign_key 등 정보가 잘 들어온 것을 확인할 수 있었다. 

 dbdiagram에서 작성한 스키마를 토대로 table plus에 한번에 불러와지니 편리하면서도 신기하다.