File: deletion.rst

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (50 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
.. include:: ./snippets/postgres_doc_links.rst

Deletion
========

.. _truncate_page:

Truncate
--------
In standard Django, deleting all records in a table is quite slow and cumbersome. It requires retrieving all rows from the database and deleting them one by one (unless you use bulk delete). Postgres has a standard statement for emptying out a table: `TRUNCATE TABLE`_.

Using the :meth:`~psqlextra.manager.PostgresManager.truncate` method on the :class:`~psqlextra.manager.PostgresManager` allows you to delete all records in a table in the blink of an eye:

.. code-block:: python

   from django.db import models
   from psqlextra.models import PostgresModel

   class MyModel(PostgresModel):
       myfield = models.CharField(max_length=255, unique=True)

   MyModel.objects.create(myfield="1")

   MyModel.objects.truncate() # table is empty after this
   print(MyModel.objects.count()) # zero records left


Cascade
*******

By default, Postgres will raise an error if any other table is referencing one of the rows you're trying to delete. One can tell Postgres to cascade the truncate operation to all related rows.

.. code-block:: python

   from django.db import models
   from psqlextra.models import PostgresModel

   class MyModel1(PostgresModel):
       myfield = models.CharField(max_length=255, unique=True)


   class MyModel2(PostgresModel):
       mymodel1 = models.ForeignKey(Model1, on_delete=models.CASCADE)

   obj1 = MyModel1.objects.create(myfield="1")
   MyModel2.objects.create(mymodel1=obj1)

   MyModel.objects.truncate(cascade=True)
   print(MyModel1.objects.count()) # zero records left
   print(MyModel2.objects.count()) # zero records left