Skip to content

Commit 72a6070

Browse files
committed
Very clearly specify that FK to partitioned models are not supported
1 parent 552fb27 commit 72a6070

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

docs/source/table_partitioning.rst

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ Known limitations
2828

2929
Foreign keys
3030
~~~~~~~~~~~~
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
3432

3533
Foreing keys **on** a partitioned models to other, non-partitioned models are always supported.
3634

@@ -118,27 +116,65 @@ Primary key
118116

119117
PostgreSQL demands that the primary key is the same or is part of the partitioning key. See `PostgreSQL Table Partitioning Limitations`_.
120118

121-
TL;DR Foreign keys don't work in Django <5.2. Use Django 5.2 or newer for proper support.
122-
123119
**In Django <5.2, the behavior is as following:**
124120

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.
128122

129123
- If the primary key is not the exact same as the partitioning key or the partitioning key consists of more than one field:
130124

131125
An implicit composite primary key (not visible from Django) is created.
132126

133-
Foreign keys to partitioned tables will **NOT** work.
134-
135127
**In Django >5.2, the behavior is as following:**
136128

137129
- 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.
138130

139131
- 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.
140132

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+
class MyModel(PostgresPartitionedModel):
147+
class PartitioningMeta:
148+
method = PostgresPartitioningMethod.RANGE
149+
key = ["timestamp"]
150+
151+
# WARNING: This overrides default primary key that includes a auto-increment `id` field.
152+
pk = models.CompositePrimaryKey("name", "timestamp")
153+
154+
name = models.TextField()
155+
timestamp = models.DateTimeField()
156+
157+
158+
Custom composite primary key with auto-incrementing ID
159+
""""""""""""""""""""""""""""""""""""""""""""""""""""""
160+
161+
.. code-block:: python
162+
163+
from django.db import models
164+
165+
from psqlextra.types import PostgresPartitioningMethod
166+
from psqlextra.models import PostgresPartitionedModel
167+
168+
class MyModel(PostgresPartitionedModel):
169+
class PartitioningMeta:
170+
method = PostgresPartitioningMethod.RANGE
171+
key = ["timestamp"]
172+
173+
id = models.AutoField(primary_key=True)
174+
pk = models.CompositePrimaryKey("id", "timestamp")
175+
176+
name = models.TextField()
177+
timestamp = models.DateTimeField()
142178
143179
144180
Generating a migration

0 commit comments

Comments
 (0)