{{{
## 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.
Old description:
New description:
PR: https://github.com/django/django/pull/15542
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:1>
* 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>
* owner: nobody => Mariusz Felisiak
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:3>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/15952 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/33919#comment:4>
* 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>
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>