summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/schema.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-09-29 13:20:14 +0200
committerGitHub <noreply@github.com>2022-09-29 13:20:14 +0200
commit19e6efa50b603af325e7f62058364f278596758f (patch)
tree040b2ad07430c84430233d3d34b2ee0091d34e25 /django/db/backends/postgresql/schema.py
parent468d06109778e3656229ac28c9019ea7246f9b79 (diff)
downloaddjango-19e6efa50b603af325e7f62058364f278596758f.tar.gz
Fixed #34058 -- Changed sequence types when altering pre-Django 4.1 auto fields on PostgreSQL.
Thanks Anders Kaseorg for the report. Thanks Florian Apolloner for pair programming. Regression in 2eea361eff58dd98c409c5227064b901f41bd0d6.
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
-rw-r--r--django/db/backends/postgresql/schema.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index 98af319570..d512ecefb6 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -13,7 +13,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
"UPDATE %(table)s SET %(column)s = %(default)s WHERE %(column)s IS NULL"
"; SET CONSTRAINTS ALL IMMEDIATE"
)
-
+ sql_alter_sequence_type = "ALTER SEQUENCE IF EXISTS %(sequence)s AS %(type)s"
sql_delete_sequence = "DROP SEQUENCE IF EXISTS %(sequence)s CASCADE"
sql_create_index = (
@@ -208,6 +208,29 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
[],
),
]
+ elif new_is_auto and old_is_auto and old_internal_type != new_internal_type:
+ fragment, _ = super()._alter_column_type_sql(
+ model, old_field, new_field, new_type
+ )
+ column = strip_quotes(new_field.column)
+ sequence_name = f"{table}_{column}_seq"
+ db_types = {
+ "AutoField": "integer",
+ "BigAutoField": "bigint",
+ "SmallAutoField": "smallint",
+ }
+ return fragment, [
+ # Alter the sequence type if exists (Django 4.1+ identity
+ # columns don't have it).
+ (
+ self.sql_alter_sequence_type
+ % {
+ "sequence": self.quote_name(sequence_name),
+ "type": db_types[new_internal_type],
+ },
+ [],
+ ),
+ ]
else:
return super()._alter_column_type_sql(model, old_field, new_field, new_type)