﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33919	Primary keys added in non-initial migration are not created as identity columns in PostgreSQL	jackcbrown89	Mariusz Felisiak	"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
PR: https://github.com/django/django/pull/15542
"	Bug	closed	Database layer (models, ORM)	4.1	Release blocker	fixed	postgres	Florian Apolloner Michael Kany	Accepted	1	0	0	0	0	0
