[Django] external key cascade deletion in ORM

Source: Internet
Author: User

The system environment here is django1.6 OS for win7

Today, we need to add a region table and associate it with foreign keys and several important entity tables (the region table is used as the foreign key of other tables ), when the delete operation of the region is completed, the code is tested and the function is normal. But it's silly to move your eyes to the console for output.

connexesql ->DELETE FROM `mngm_device` WHERE `mngm_device`.`area_id` IN (%s, %s, %s)connexesql ->DELETE FROM `ad_ad` WHERE `ad_ad`.`area_id` IN (%s, %s, %s)connexesql ->DELETE FROM `mngm_area` WHERE `id` IN (%s, %s, %s)

I deleted the object associated with the region first !!! This region is only an affiliated attribute. When deleting it, it actually deletes more important entities, which is a serious problem.

As a result, It queries the file about each field definition in models in Django Orm, https://docs.djangoproject.com/en/1.6/ref/models/fields/#django.db.models.ForeignKey. Then, modify the field definition to the following:

user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)

When the area is deleted, associate other tables with this field and set it to null. In this way, the area record is deleted.

Django foreign key tips:
* The foreign key is associated with an object. The automatically generated foreign key name is appended with the name of the associated object with "_ id" as the field name.
* By default, the foreign key will automatically create an index and perform cascading deletion. The default foreign key is that the field is the primary key of the table.

Models. set_null there are several other options:

* Set_null when the field of the foreign key is deleted, it is set to null. If null = true is specified

* Default cascade option. When a field associated with a foreign key is deleted, all other tables are deleted in cascade mode.

* Set_default: Set a default value. When the associated record is deleted, it is restored to the default value.

* Do_nothing Django does not do anything. If the database returns anything, it will report anything.

* Set ()? You can also set a function to trigger a value when the associated record is deleted.


There are always a lot of default settings for the ORM or framework. When we develop it, we must test and read more documents for better use, so as not to make some amazing mistakes.


This article from the "orangleliu notebook" blog, reprint please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/40268495

[Django] external key cascade deletion in ORM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.