[Django] #33919: Primary keys added in non-initial migration are not created as identity columns in PostgreSQL

3 views
Skip to first unread message

Django

unread,
Aug 11, 2022, 2:20:27 PM8/11/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-----------------------------------------+--------------------------
Reporter: jackcbrown89 | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Keywords: postgres
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+--------------------------
In this example, a model was created initially with a primary key on a
one-to-one field. The primary key was then moved to a new AutoField. This
was working prior to the 4.1 release.

{{{
## models.py
class Example(models.Model):
old_id = models.OneToOneField(
OtherModel,
on_delete=models.CASCADE,
## primary_key=True, <--- used to be the primary key
)

id = models.AutoField(primary_key=True) # <--- new primary key
}}}

Running sqlmigrate on the migration where the primary key changes reveals
that the "id" column does not autogenerate values:
{{{
$ python manage.py sqlmigrate example 0002

BEGIN;
--
-- Add field id to example
--
ALTER TABLE "example" ADD COLUMN "id" integer NOT NULL PRIMARY KEY;
--
-- Alter field old_id on example
--
ALTER TABLE "example" ALTER COLUMN "old_id" DROP NOT NULL;
}}}

When it should be
{{{
$ python manage.py sqlmigrate example 0002

BEGIN;
--
-- Add field id to example
--
ALTER TABLE "example" ADD COLUMN "id" integer NOT NULL PRIMARY KEY
GENERATED BY DEFAULT AS IDENTITY;
--
-- Alter field old_id on example
--
ALTER TABLE "example" ALTER COLUMN "old_id" DROP NOT NULL;
}}}

This seems to be related to the changes made here:
https://code.djangoproject.com/ticket/30511

--
Ticket URL: <https://code.djangoproject.com/ticket/33919>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 11, 2022, 2:22:42 PM8/11/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-------------------------------+--------------------------------------

Reporter: jackcbrown89 | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:

Keywords: postgres | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by jackcbrown89:

Old description:

New description:

PR: https://github.com/django/django/pull/15542

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:1>

Django

unread,
Aug 12, 2022, 12:38:32 AM8/12/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jackcbrown89 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: postgres | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* type: Uncategorized => Bug
* component: Migrations => Database layer (models, ORM)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Thanks for the report! Column suffixes (as `GENERATED BY...`) should be
used when adding a new field:
{{{#!diff
diff --git a/django/db/backends/base/schema.py
b/django/db/backends/base/schema.py
index 5e111aed73..f9bf6ed236 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -637,6 +637,9 @@ class BaseDatabaseSchemaEditor:
# It might not actually have a column behind it
if definition is None:
return
+ col_type_suffix =
field.db_type_suffix(connection=self.connection)
+ if col_type_suffix:
+ definition += f" {col_type_suffix}"
# Check constraints can go on the column SQL here
db_params = field.db_parameters(connection=self.connection)
if db_params["check"]:

}}}

Regression in 2eea361eff58dd98c409c5227064b901f41bd0d6.

--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:2>

Django

unread,
Aug 12, 2022, 12:56:41 AM8/12/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jackcbrown89 | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned

Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Mariusz Felisiak
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:3>

Django

unread,
Aug 12, 2022, 1:07:33 AM8/12/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jackcbrown89 | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/15952 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:4>

Django

unread,
Aug 12, 2022, 11:30:45 AM8/12/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jackcbrown89 | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed

Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"5c803bc0702511c8bc05e9db600367a465514f82" 5c803bc]:
{{{
#!CommitTicketReference repository=""
revision="5c803bc0702511c8bc05e9db600367a465514f82"
Fixed #33919 -- Fixed adding AutoFields on PostgreSQL.

Thanks Jack Calvin Brown for the report.

Regression in 2eea361eff58dd98c409c5227064b901f41bd0d6.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:5>

Django

unread,
Aug 12, 2022, 11:32:22 AM8/12/22
to django-...@googlegroups.com
#33919: Primary keys added in non-initial migration are not created as identity
columns in PostgreSQL
-------------------------------------+-------------------------------------
Reporter: jackcbrown89 | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"3848475eeb5ee8f7729440f50c04fd85cf8bea66" 3848475e]:
{{{
#!CommitTicketReference repository=""
revision="3848475eeb5ee8f7729440f50c04fd85cf8bea66"
[4.1.x] Fixed #33919 -- Fixed adding AutoFields on PostgreSQL.

Thanks Jack Calvin Brown for the report.

Regression in 2eea361eff58dd98c409c5227064b901f41bd0d6.
Backport of 5c803bc0702511c8bc05e9db600367a465514f82 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:6>

Reply all
Reply to author
Forward
0 new messages