diff options
| author | Tim Graham <timograham@gmail.com> | 2016-01-07 20:06:58 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-01-08 14:47:05 -0500 |
| commit | f8c3d38c2d26f99ca620d76a0d88b69eeeb88c2c (patch) | |
| tree | b5d5c40cfb6deb3573ad72e6456d897ce875fe37 /tests | |
| parent | fe5d37f99145f5bbd139d6c0729d98040c3ee774 (diff) | |
[1.8.x] Fixed #26034 -- Fixed incorrect index handling on PostgreSQL on Char/TextField with unique=True and db_index=True.
Thanks Simon Charette for review.
Backport of 56aaae58a746eb39d5e92ba60f59f4c750a8e1a8 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/schema/tests.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 601350409c..c5aa0927ad 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -1592,3 +1592,45 @@ 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(len(self.get_constraints_for_column(BookWithoutAuthor, 'title')), 2) + # 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(len(self.get_constraints_for_column(BookWithoutAuthor, 'title')), 3) + # 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(len(self.get_constraints_for_column(BookWithoutAuthor, 'title')), 3) + + @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(len(self.get_constraints_for_column(Tag, 'slug')), 2) + # 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(len(self.get_constraints_for_column(Tag, 'slug')), 2) + # 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(len(self.get_constraints_for_column(Tag, 'slug')), 2) |
