기본 콘텐츠로 건너뛰기

update a parent row: a foreign key constraint fails in flask-migrate

update a parent row: a foreign key constraint fails in flask-migrate

Flask 에서 DB Class 코드를 지운 후

migration 후 upgrade 명령어를 수행하는 중에

'Cannot delete or update a parent row: a foreign key constraint fails' 다음과 같은 에러가 발생했다.

foreignkey 관계의 같은 경우

ex ) post - comment 인 경우 post가 삭제 될 때 comment 를 casecade 로 삭제하도록 설정하지 않은 경우

위와 같은 에러가 발생할 수 있다.

위와 같은 경우는 mysql 서버에 접속한 후

SET Global FOREIGN_KEY_CHECKS=0;

global 을 붙이지 않으면 shell 에서 python manage.py db upgrade 명령시에는 적용되지 않고,

mysql 서버에서만 해당 테이블을 삭제할 수 있기 때문에 global 을 붙여줘야 한다.

명령어를 입력하면 foreign_key 관게를 확인하지 않고 db table을 삭제해준다.

DB를 삭제한 후에는 다시 SET Global FOREIGN_KEY_CHECKS=1;

다시 원상태로 복구하는 것이 좋다.

누군가에는 도움이 되길..!

from http://blog.doosikbae.com/74 by ccl(A) rewrite - 2020-03-07 11:54:53

댓글

이 블로그의 인기 게시물

Flask 13. pythonanywhere에 배포하기

Flask 13. pythonanywhere에 배포하기 Login: PythonAnywhere It's always a pleasure to hear from you! Ask us a question, or tell us what you love or hate about PythonAnywhere. We'll get back to you over email ASAP. Sorry, there was an error connecting to the server. Please try again in a few moments... www.pythonanywhere.com from http://ohdowon064.tistory.com/124 by ccl(A) rewrite - 2020-03-11 15:54:10

[ubuntu] FLASK_APP

[ubuntu] FLASK_APP Development/Debugging 🐞 FLASK_ENV=development FLASK_APP = app.py flask run zsh: command not found: FLASK_APP ✔️ FLASK_ENV=development FLASK_APP=app.py flask run 띄어쓰기를 해서 저런 오류를 출력할수도 있구나 😲 참고 : 108p에서 FLASK가 FKAS로 오타나있다. from http://hee-stories.tistory.com/18 by ccl(A) rewrite - 2020-03-24 17:20:11