xxxxxxxxxx
The on_delete=models.CASCADE tells Django to cascade the deleting effect
i.e. continue deleting the dependent models as well.
xxxxxxxxxx
on_delete=models.CASCADE will delete anything created by the admin if
the admin user is deleted.
xxxxxxxxxx
When the referenced object is deleted, all objects that have
a foreign key to it will also be deleted.
For example, let's say we have an Author named "John" and two
books associated with him. If we delete the "John" Author object,
the two Book objects associated with "John" will also be
deleted automatically.
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
xxxxxxxxxx
CREATE TBALE parent_table_p (
id INT PRIMARY KEY
)
CREATE_TABLE child_table_c(
id INT PRIMARY KEY,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parent_table_p(id) ON DELETE CASCADE
)
xxxxxxxxxx
The ON DELETE CASCADE clause specifies that if a row in the
table A is deleted, any rows in the table B that reference the deleted
row will also be deleted.
xxxxxxxxxx
CREATE TABLE child_table
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, parent_col_n)
ON DELETE CASCADE
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
);
xxxxxxxxxx
CREATE TABLE Enroll (
sno INT,
cno INT,
jdate date,
PRIMARY KEY(sno,cno),
FOREIGN KEY(sno)
REFERENCES Student(sno)
ON DELETE CASCADE
FOREIGN KEY(cno)
REFERENCES Course(cno)
ON DELETE CASCADE
);