Posts

Showing posts with the label Models

Django Models Foreign Key Integrity

Image
Django database integrity: ForeignKey on_delete options Django comes with a very robust Object Relational Mapper(ORM) for managing Relational Management Database Management Systems. I will be writing about Django’s  ForeignKey  on_delete options for determining how deletion of referenced data is handled by the database so as to maintain data integrity. The options for the  on_delete  include: 1 CASCADE 2 PROTECT 3 SET_NULL 4 SET_DEFAULT 5 SET() 6 DO_NOTHING We are going to create two models to demonstrate the use cases for the above options. A  Post  and a  Comment  model. I have defined the serializers and views for the post and comment respectively. For brevity, I have omitted them. I am using the browerable API to make the requests to the server. I have a DB Browser for SQLite and I will show the contents of the database as we progress. CASCADE Cascade emulates the SQL constraint of  ON DELETE CASCADE . Whenever the referenced object (post) is