summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-08-01 09:21:07 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-08-01 09:25:33 +0200
commit3dac3271d286f2790780e89d31ddbb7197f8defa (patch)
tree79c692137c241e587c82dea21e3d76b2afe320d6 /django/db
parent8cf931dd2fde2561a615b96d5f709c15b672c4ba (diff)
Reverted "Fixed #28646 -- Prevented duplicate index when unique is set to True on PostgreSQL."
This reverts commit 9cf9c796be8dd53bc3b11355ff39d65c81d7be6d due to a crash on Oracle as it didn't allow multiple indexes on the same field.
Diffstat (limited to 'django/db')
-rw-r--r--django/db/backends/base/schema.py4
-rw-r--r--django/db/backends/postgresql/schema.py38
2 files changed, 10 insertions, 32 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index f6b675339a..3e38c56d50 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -964,7 +964,9 @@ class BaseDatabaseSchemaEditor:
fks_dropped.add((old_field.column,))
self.execute(self._delete_fk_sql(model, fk_name))
# Has unique been removed?
- if old_field.unique and not old_field.primary_key and not new_field.unique:
+ if old_field.unique and (
+ not new_field.unique or self._field_became_primary_key(old_field, new_field)
+ ):
# Find the unique constraint for this field
meta_constraint_names = {
constraint.name for constraint in model._meta.constraints
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index c0a785c7e1..0c8548a5d6 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -140,13 +140,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return sequence["name"]
return None
- def _is_changing_type_of_indexed_text_column(self, old_field, old_type, new_type):
- return (old_field.db_index or old_field.unique) and (
- (old_type.startswith("varchar") and not new_type.startswith("varchar"))
- or (old_type.startswith("text") and not new_type.startswith("text"))
- or (old_type.startswith("citext") and not new_type.startswith("citext"))
- )
-
def _alter_column_type_sql(
self, model, old_field, new_field, new_type, old_collation, new_collation
):
@@ -154,7 +147,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# different type.
old_db_params = old_field.db_parameters(connection=self.connection)
old_type = old_db_params["type"]
- if self._is_changing_type_of_indexed_text_column(old_field, old_type, new_type):
+ if (old_field.db_index or old_field.unique) and (
+ (old_type.startswith("varchar") and not new_type.startswith("varchar"))
+ or (old_type.startswith("text") and not new_type.startswith("text"))
+ or (old_type.startswith("citext") and not new_type.startswith("citext"))
+ ):
index_name = self._create_index_name(
model._meta.db_table, [old_field.column], suffix="_like"
)
@@ -258,25 +255,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
model, old_field, new_field, new_type, old_collation, new_collation
)
- def _new_index_should_be_added(self, old_field, new_field):
- return not (old_field.db_index or old_field.unique) and (
- new_field.db_index or new_field.unique
- )
-
- def _deleted_index_should_be_recreated(
- self, old_field, new_field, old_type, new_type
- ):
- if (
- not old_field.unique
- and (
- not new_field.db_index
- or (new_field.unique and not new_field.primary_key)
- )
- ) or (
- self._is_changing_type_of_indexed_text_column(old_field, old_type, new_type)
- ):
- return True
-
def _alter_field(
self,
model,
@@ -299,10 +277,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
strict,
)
# Added an index? Create any PostgreSQL-specific indexes.
- if self._new_index_should_be_added(
- old_field, new_field
- ) or self._deleted_index_should_be_recreated(
- old_field, new_field, old_type, new_type
+ if (not (old_field.db_index or old_field.unique) and new_field.db_index) or (
+ not old_field.unique and new_field.unique
):
like_index_statement = self._create_like_index_sql(model, new_field)
if like_index_statement is not None: