Ticket #35916: 0001-Warn-about-surprising-behavior-of-automatic-primary-.patch

File 0001-Warn-about-surprising-behavior-of-automatic-primary-.patch, 1.3 KB (added by Eric Hanchrow, 6 days ago)
  • docs/topics/db/models.txt

    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  
    294294
    295295.. _verbose-field-names:
    296296
     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
    297313Verbose field names
    298314-------------------
    299315
Back to Top