foreign key constraint not letting delete row from table in django
<p>I create below model in Django.</p>

<pre><code>class ClientsModel(models.Model):
    sys_id = models.AutoField(primary_key=True, null=False, blank=True)
    tenant_sys_id = models.ForeignKey('tenant.TenantModel', on_delete = models.SET_NULL, null=True, blank=True)
    last_name = models.CharField(max_length=40, null=True, blank=True)
    # and few more rows.
</code></pre>

<p>Here tenant_sys_id is a foreign key. I set on_delete = models.SET_NULL, which means if I delete any entry from TenantModel (lets say entry1) then column tenant_sys_id in all rows having tenant_sys_id as entry1 should be set to null. I am right here?<br>
If yes- Then here is next question. If I try to delete an entry from tenants table from mysql then it is throwing me below error.</p>

<pre><code>mysql&gt; delete from tenants where sys_id = 1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`sanstha_db`.`clients`, CONSTRAINT `Clients_tenant_sys_id_id_52191b87_fk_Tenants_sys_id` FOREIGN KEY (`tenant_sys_id_id`) REFERENCES `tenants` (`sys_id`))
</code></pre>

<p>Here is the table structure.</p>

<pre><code>Table: clients
Create Table: CREATE TABLE `clients` (
  `sys_id` int(11) NOT NULL AUTO_INCREMENT,
  `last_name` varchar(40) DEFAULT NULL,
  `first_name` varchar(40) NOT NULL,
  `middle_name` varchar(40) DEFAULT NULL,
  `display_name` varchar(140) NOT NULL,
  `is_business` tinyint(1) NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `is_vendor` tinyint(1) NOT NULL,
  `is_authorized_to_expense` tinyint(1) NOT NULL,
  `relation` bigint(20) DEFAULT NULL,
  `dob` date DEFAULT NULL,
  `gender` bigint(20) DEFAULT NULL,
  `created_when` datetime DEFAULT NULL,
  `created_by` bigint(20) DEFAULT NULL,
  `last_updated_when` datetime DEFAULT NULL,
  `last_updated_by` bigint(20) DEFAULT NULL,
  `notes` varchar(2048) DEFAULT NULL,
  `head_of_family_id` int(11) DEFAULT NULL,
  `tenant_sys_id_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`sys_id`),
  KEY `Clients_head_of_family_id_01c17980_fk_Clients_sys_id` (`head_of_family_id`),
  KEY `Clients_tenant_sys_id_id_52191b87_fk_Tenants_sys_id` (`tenant_sys_id_id`),
  CONSTRAINT `Clients_head_of_family_id_01c17980_fk_Clients_sys_id` FOREIGN KEY (`head_of_family_id`) REFERENCES `clients` (`sys_id`),
  CONSTRAINT `Clients_tenant_sys_id_id_52191b87_fk_Tenants_sys_id` FOREIGN KEY (`tenant_sys_id_id`) REFERENCES `tenants` (`sys_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1
</code></pre>

<p>Please suggest me why it is not letting me delete the entry? what is the correct way to do it.</p>