Refs #30511 -- Updated docs about auto-incrementing primary keys on PostgreSQL.

Follow up to 2eea361eff.
This commit is contained in:
Mariusz Felisiak 2022-08-26 21:42:44 +02:00 committed by GitHub
parent 166a3b3263
commit 081871bc20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 13 deletions

View File

@ -274,7 +274,7 @@ readability):
-- Create model Question -- Create model Question
-- --
CREATE TABLE "polls_question" ( CREATE TABLE "polls_question" (
"id" serial NOT NULL PRIMARY KEY, "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
"question_text" varchar(200) NOT NULL, "question_text" varchar(200) NOT NULL,
"pub_date" timestamp with time zone NOT NULL "pub_date" timestamp with time zone NOT NULL
); );
@ -282,10 +282,10 @@ readability):
-- Create model Choice -- Create model Choice
-- --
CREATE TABLE "polls_choice" ( CREATE TABLE "polls_choice" (
"id" serial NOT NULL PRIMARY KEY, "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
"choice_text" varchar(200) NOT NULL, "choice_text" varchar(200) NOT NULL,
"votes" integer NOT NULL, "votes" integer NOT NULL,
"question_id" integer NOT NULL "question_id" bigint NOT NULL
); );
ALTER TABLE "polls_choice" ALTER TABLE "polls_choice"
ADD CONSTRAINT "polls_choice_question_id_c5b4b260_fk_polls_question_id" ADD CONSTRAINT "polls_choice_question_id_c5b4b260_fk_polls_question_id"
@ -315,10 +315,10 @@ Note the following:
PostgreSQL to not enforce the foreign key until the end of the transaction. PostgreSQL to not enforce the foreign key until the end of the transaction.
* It's tailored to the database you're using, so database-specific field types * It's tailored to the database you're using, so database-specific field types
such as ``auto_increment`` (MySQL), ``serial`` (PostgreSQL), or ``integer such as ``auto_increment`` (MySQL), ``bigint PRIMARY KEY GENERATED BY DEFAULT
primary key autoincrement`` (SQLite) are handled for you automatically. Same AS IDENTITY`` (PostgreSQL), or ``integer primary key autoincrement`` (SQLite)
goes for the quoting of field names -- e.g., using double quotes or are handled for you automatically. Same goes for the quoting of field names
single quotes. -- e.g., using double quotes or single quotes.
* The :djadmin:`sqlmigrate` command doesn't actually run the migration on your * The :djadmin:`sqlmigrate` command doesn't actually run the migration on your
database - instead, it prints it to the screen so that you can see what SQL database - instead, it prints it to the screen so that you can see what SQL

View File

@ -258,7 +258,7 @@ This command should produce the following output:
-- Create model WorldBorder -- Create model WorldBorder
-- --
CREATE TABLE "world_worldborder" ( CREATE TABLE "world_worldborder" (
"id" bigserial NOT NULL PRIMARY KEY, "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
"name" varchar(50) NOT NULL, "name" varchar(50) NOT NULL,
"area" integer NOT NULL, "area" integer NOT NULL,
"pop2005" integer NOT NULL, "pop2005" integer NOT NULL,

View File

@ -291,9 +291,9 @@ live for the duration of the transaction.
Manually-specifying values of auto-incrementing primary keys Manually-specifying values of auto-incrementing primary keys
------------------------------------------------------------ ------------------------------------------------------------
Django uses PostgreSQL's `SERIAL data type`_ to store auto-incrementing primary Django uses PostgreSQL's identity columns to store auto-incrementing primary
keys. A ``SERIAL`` column is populated with values from a `sequence`_ that keys. An identity column is populated with values from a `sequence`_ that keeps
keeps track of the next available value. Manually assigning a value to an track of the next available value. Manually assigning a value to an
auto-incrementing field doesn't update the field's sequence, which might later auto-incrementing field doesn't update the field's sequence, which might later
cause a conflict. For example:: cause a conflict. For example::
@ -310,7 +310,11 @@ If you need to specify such values, reset the sequence afterward to avoid
reusing a value that's already in the table. The :djadmin:`sqlsequencereset` reusing a value that's already in the table. The :djadmin:`sqlsequencereset`
management command generates the SQL statements to do that. management command generates the SQL statements to do that.
.. _SERIAL data type: https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL .. versionchanged:: 4.1
In older versions, PostgreSQLs ``SERIAL`` data type was used instead of
identity columns.
.. _sequence: https://www.postgresql.org/docs/current/sql-createsequence.html .. _sequence: https://www.postgresql.org/docs/current/sql-createsequence.html
Test database templates Test database templates

View File

@ -39,7 +39,7 @@ The above ``Person`` model would create a database table like this:
.. code-block:: sql .. code-block:: sql
CREATE TABLE myapp_person ( CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY, "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
"first_name" varchar(30) NOT NULL, "first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL "last_name" varchar(30) NOT NULL
); );