mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #26034 -- Fixed incorrect index handling on PostgreSQL on Char/TextField with unique=True and db_index=True.
Thanks Simon Charette for review.
This commit is contained in:
parent
54d3ba8406
commit
56aaae58a7
4 changed files with 72 additions and 2 deletions
|
@ -1757,3 +1757,63 @@ class SchemaTests(TransactionTestCase):
|
|||
with connection.schema_editor() as editor:
|
||||
editor.alter_field(Note, new_field, old_field, strict=True)
|
||||
self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
|
||||
|
||||
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
||||
def test_alter_field_add_unique_to_charfield_with_db_index(self):
|
||||
# Create the table and verify initial indexes.
|
||||
with connection.schema_editor() as editor:
|
||||
editor.create_model(BookWithoutAuthor)
|
||||
self.assertEqual(
|
||||
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
||||
['schema_book_d5d3db17', 'schema_book_title_2dfb2dff_like']
|
||||
)
|
||||
# Alter to add unique=True (should add 1 index)
|
||||
old_field = BookWithoutAuthor._meta.get_field('title')
|
||||
new_field = CharField(max_length=100, db_index=True, unique=True)
|
||||
new_field.set_attributes_from_name('title')
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
|
||||
self.assertEqual(
|
||||
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
||||
['schema_book_d5d3db17', 'schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
|
||||
)
|
||||
# Alter to remove unique=True (should drop unique index) # XXX: bug!
|
||||
old_field = BookWithoutAuthor._meta.get_field('title')
|
||||
new_field = CharField(max_length=100, db_index=True)
|
||||
new_field.set_attributes_from_name('title')
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
|
||||
self.assertEqual(
|
||||
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
||||
['schema_book_d5d3db17', 'schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
|
||||
)
|
||||
|
||||
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
||||
def test_alter_field_add_db_index_to_charfield_with_unique(self):
|
||||
# Create the table and verify initial indexes.
|
||||
with connection.schema_editor() as editor:
|
||||
editor.create_model(Tag)
|
||||
self.assertEqual(
|
||||
self.get_constraints_for_column(Tag, 'slug'),
|
||||
['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
|
||||
)
|
||||
# Alter to add db_index=True
|
||||
old_field = Tag._meta.get_field('slug')
|
||||
new_field = SlugField(db_index=True, unique=True)
|
||||
new_field.set_attributes_from_name('slug')
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_field(Tag, old_field, new_field, strict=True)
|
||||
self.assertEqual(
|
||||
self.get_constraints_for_column(Tag, 'slug'),
|
||||
['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
|
||||
)
|
||||
# Alter to remove db_index=True
|
||||
old_field = Tag._meta.get_field('slug')
|
||||
new_field = SlugField(unique=True)
|
||||
new_field.set_attributes_from_name('slug')
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_field(Tag, old_field, new_field, strict=True)
|
||||
self.assertEqual(
|
||||
self.get_constraints_for_column(Tag, 'slug'),
|
||||
['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue