Fixed #33125 -- Avoided redundant unique constraint when converting a non-unique field to primary key on MySQL and PostgreSQL.
This commit is contained in:
parent
4ffada3609
commit
f5a3243911
|
@ -1155,8 +1155,10 @@ class BaseDatabaseSchemaEditor:
|
||||||
return not old_field.primary_key and new_field.primary_key
|
return not old_field.primary_key and new_field.primary_key
|
||||||
|
|
||||||
def _unique_should_be_added(self, old_field, new_field):
|
def _unique_should_be_added(self, old_field, new_field):
|
||||||
return (not old_field.unique and new_field.unique) or (
|
return (
|
||||||
old_field.primary_key and not new_field.primary_key and new_field.unique
|
not new_field.primary_key and
|
||||||
|
new_field.unique and
|
||||||
|
(not old_field.unique or old_field.primary_key)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _rename_field_sql(self, table, old_field, new_field, new_type):
|
def _rename_field_sql(self, table, old_field, new_field, new_type):
|
||||||
|
|
|
@ -174,12 +174,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
||||||
return False
|
return False
|
||||||
return create_index
|
return create_index
|
||||||
|
|
||||||
def _unique_should_be_added(self, old_field, new_field):
|
|
||||||
return (
|
|
||||||
super()._unique_should_be_added(old_field, new_field) and
|
|
||||||
not self._field_became_primary_key(old_field, new_field)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _is_identity_column(self, table_name, column_name):
|
def _is_identity_column(self, table_name, column_name):
|
||||||
with self.connection.cursor() as cursor:
|
with self.connection.cursor() as cursor:
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
|
|
|
@ -741,6 +741,13 @@ class SchemaTests(TransactionTestCase):
|
||||||
with connection.schema_editor() as editor:
|
with connection.schema_editor() as editor:
|
||||||
editor.remove_field(Author, Author._meta.get_field('id'))
|
editor.remove_field(Author, Author._meta.get_field('id'))
|
||||||
editor.alter_field(Author, old_field, new_field, strict=True)
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
||||||
|
# Redundant unique constraint is not added.
|
||||||
|
count = self.get_constraints_count(
|
||||||
|
Author._meta.db_table,
|
||||||
|
Author._meta.get_field('uuid').column,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
self.assertLessEqual(count['uniques'], 1)
|
||||||
|
|
||||||
@isolate_apps('schema')
|
@isolate_apps('schema')
|
||||||
def test_alter_primary_key_quoted_db_table(self):
|
def test_alter_primary_key_quoted_db_table(self):
|
||||||
|
|
Loading…
Reference in New Issue