summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/schema.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-10-01 07:53:32 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-10-01 07:59:28 +0200
commit96c541ecef98e2b93db8eba144be2d8c48e6c2cf (patch)
tree1072b7ffefd0686935dd8c868e041820100f918c /django/db/backends/postgresql/schema.py
parent7a1675806a37375698df208c00f892cc81afe1b9 (diff)
[4.1.x] Refs #34058 -- Fixed changing/deleting sequences when altering pre-Django 4.1 auto fields on PostgreSQL.
Thanks Anders Kaseorg for the report. Follow up to 19e6efa50b603af325e7f62058364f278596758f. Regression in 2eea361eff58dd98c409c5227064b901f41bd0d6. Backport of bc3b8f152452ba0e41f28baa93c0bf8f39cddb09 from main
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
-rw-r--r--django/db/backends/postgresql/schema.py61
1 files changed, 36 insertions, 25 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index fa6e0a8ebf..d95d7557f7 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -112,6 +112,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
)
return None
+ def _get_sequence_name(self, table, column):
+ with self.connection.cursor() as cursor:
+ for sequence in self.connection.introspection.get_sequences(cursor, table):
+ if sequence["column"] == column:
+ return sequence["name"]
+ return None
+
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
self.sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s"
# Cast when data type changed.
@@ -168,44 +175,48 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
}
)
column = strip_quotes(new_field.column)
- sequence_name = "%s_%s_seq" % (table, column)
fragment, _ = super()._alter_column_type_sql(
model, old_field, new_field, new_type
)
- return fragment, [
- (
- # Drop the sequence if exists (Django 4.1+ identity columns
- # don't have it).
- self.sql_delete_sequence
- % {
- "sequence": self.quote_name(sequence_name),
- },
- [],
- ),
- ]
+ # Drop the sequence if exists (Django 4.1+ identity columns don't
+ # have it).
+ other_actions = []
+ if sequence_name := self._get_sequence_name(table, column):
+ other_actions = [
+ (
+ self.sql_delete_sequence
+ % {
+ "sequence": self.quote_name(sequence_name),
+ },
+ [],
+ )
+ ]
+ return fragment, other_actions
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],
- },
- [],
- ),
- ]
+ # Alter the sequence type if exists (Django 4.1+ identity columns
+ # don't have it).
+ other_actions = []
+ if sequence_name := self._get_sequence_name(table, column):
+ other_actions = [
+ (
+ self.sql_alter_sequence_type
+ % {
+ "sequence": self.quote_name(sequence_name),
+ "type": db_types[new_internal_type],
+ },
+ [],
+ ),
+ ]
+ return fragment, other_actions
else:
return super()._alter_column_type_sql(model, old_field, new_field, new_type)