You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/table_partitioning.rst
+47-11Lines changed: 47 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,9 +28,7 @@ Known limitations
28
28
29
29
Foreign keys
30
30
~~~~~~~~~~~~
31
-
Support for foreign keys to partitioned models is limited in Django 5.1 and older. These are only suported under specific conditions.
32
-
33
-
For full support for foreign keys to partitioned models, use Django 5.2 or newer. Django 5.2 supports composite primary and foreign keys native through :class:`~django:django.db.models.CompositePrimaryKey` to support.
31
+
There is no support for foreign keys **to** partitioned models. Even in Django 5.2 with the introduction of :class:`~django:django.db.models.CompositePrimaryKey`, there is no support for foreign keys. See: https://code.djangoproject.com/ticket/36034
34
32
35
33
Foreing keys **on** a partitioned models to other, non-partitioned models are always supported.
36
34
@@ -118,27 +116,65 @@ Primary key
118
116
119
117
PostgreSQL demands that the primary key is the same or is part of the partitioning key. See `PostgreSQL Table Partitioning Limitations`_.
120
118
121
-
TL;DR Foreign keys don't work in Django <5.2. Use Django 5.2 or newer for proper support.
122
-
123
119
**In Django <5.2, the behavior is as following:**
124
120
125
-
- If the primary key is the same as the partitioning key:
126
-
127
-
Foreign keys to partitioned tables will work as you expect.
121
+
- If the primary key is the same as the partitioning key, standard Django behavior applies.
128
122
129
123
- If the primary key is not the exact same as the partitioning key or the partitioning key consists of more than one field:
130
124
131
125
An implicit composite primary key (not visible from Django) is created.
132
126
133
-
Foreign keys to partitioned tables will **NOT** work.
134
-
135
127
**In Django >5.2, the behavior is as following:**
136
128
137
129
- If no explicit primary key is defined, a :class:`~django:django.db.models.CompositePrimaryKey` is created automatically that includes an auto-incrementing `id` primary key field and the partitioning keys.
138
130
139
131
- If an explicit :class:`~django:django.db.models.CompositePrimaryKey` is specified, no modifications are made to it and it is your responsibility to make sure the partitioning keys are part of the primary key.
140
132
141
-
In Django 5.2 and newer, foreign keys to partitioned models always work.
133
+
Django 5.2 examples
134
+
*******************
135
+
136
+
Custom composite primary key
137
+
""""""""""""""""""""""""""""
138
+
139
+
.. code-block:: python
140
+
141
+
from django.db import models
142
+
143
+
from psqlextra.types import PostgresPartitioningMethod
144
+
from psqlextra.models import PostgresPartitionedModel
145
+
146
+
classMyModel(PostgresPartitionedModel):
147
+
classPartitioningMeta:
148
+
method = PostgresPartitioningMethod.RANGE
149
+
key = ["timestamp"]
150
+
151
+
# WARNING: This overrides default primary key that includes a auto-increment `id` field.
0 commit comments