From de8fbe4224bcd18605bf8cd617ec2c4530e786f6 Mon Sep 17 00:00:00 2001
From: Eric Hanchrow <eric.hanchrow@gmail.com>
Date: Mon, 18 Nov 2024 09:17:11 -0800
Subject: [PATCH] Warn about surprising behavior of automatic primary keys
---
docs/topics/db/models.txt | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt
index f7f575eb3f..8a39028ff4 100644
a
|
b
|
Each model requires exactly one field to have :attr:`primary_key=True
|
294 | 294 | |
295 | 295 | .. _verbose-field-names: |
296 | 296 | |
| 297 | .. warning:: |
| 298 | |
| 299 | Assuming ``Thing`` is a model with a default auto-incrementing primary key, the behavior of the below code depends on the underlying database: |
| 300 | |
| 301 | .. code-block:: python |
| 302 | |
| 303 | Thing.objects.create(pk=2) |
| 304 | |
| 305 | Thing.objects.create() |
| 306 | Thing.objects.create() |
| 307 | |
| 308 | For example, with sqlite, we wind up with three ``Thing`` instances, with primary keys 2, 1, and 3; but with Postgresql, the last ``create`` call raises an ``IntegrityError``. |
| 309 | |
| 310 | Be sure you understand how your database handles "sequences", if you're going to create some instances with specific primary keys, and some without. |
| 311 | |
| 312 | |
297 | 313 | Verbose field names |
298 | 314 | ------------------- |
299 | 315 | |