[Django] FK on_delete 종류

Yerin·2020년 2월 17일
0

study-django

목록 보기
4/5
  1. CASCADE
  • 외래키가 바라보는 값이 삭제될때 외래키를 포함하는 모델 인스턴스(row)도 삭제된다.
  1. PROTECT
  • 외래키가 삭제될때 삭제되지 않도록 protected error를 발생시킨다.
  1. SET_NULL
  • 외래키가 바라보는 값이 삭제될때 외래키 값을 null로 바꿔준다. (null=True 일때만 가능)
  1. SET_DEFAULT
  • 외래키가 바라보는 값이 삭제될때 외래키 값을 default값으로 바꿔준다.
    (default값이 있을때만 가능)
  1. SET()
  • 외래키가 바라보는 값이 삭제될때 외래키 값을 SET에 설정된 함수 등에 의해 설정된다.
  1. DO_NOTHING
  • 외래키가 바라보는 값이 삭제될때 아무런 행동을 취하지 않는다. 참조무결성을 해칠 위험이 있다.

ref:https://lee-seul.github.io/django/backend/2018/01/28/django-model-on-delete.html


  • CASCADE: When the referenced object is deleted, also delete the objects that have references to it (When you remove a blog post for instance, you might want to delete comments as well). SQL equivalent: CASCADE.

  • PROTECT: Forbid the deletion of the referenced object. To delete it you will have to delete all objects that reference it manually. SQL equivalent: RESTRICT.

  • SET_NULL: Set the reference to NULL (requires the field to be nullable). For instance, when you delete a User, you might want to keep the comments he posted on blog posts, but say it was posted by an anonymous (or deleted) user. SQL equivalent: SET NULL.

  • SET_DEFAULT: Set the default value. SQL equivalent: SET DEFAULT.

  • SET(...): Set a given value. This one is not part of the SQL standard and is entirely handled by Django.

  • DO_NOTHING: Probably a very bad idea since this would create integrity issues in your database (referencing an object that actually doesn't exist). SQL equivalent: NO ACTION.

ref: django documentation

profile
졸꾸 !!!

0개의 댓글