diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2017-05-24 07:25:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-24 07:25:59 +0200 |
| commit | 91b2bc3e70be2632baad86488fb03cf02848b5b6 (patch) | |
| tree | ba7ea6af20be180fc0442684580c46defa86eedd /tests | |
| parent | 538bf43458a147b7edeb7118c9f325c3f59ff6fb (diff) | |
Fixed #27860 -- Dropped varchar_pattern_ops/text_pattern_ops index before altering char/text field in PostgreSQL.
Thanks Tim Graham for the review.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/schema/models.py | 7 | ||||
| -rw-r--r-- | tests/schema/tests.py | 60 |
2 files changed, 59 insertions, 8 deletions
diff --git a/tests/schema/models.py b/tests/schema/models.py index 0e04dbab87..4512d8bc01 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -17,6 +17,13 @@ class Author(models.Model): apps = new_apps +class AuthorCharFieldWithIndex(models.Model): + char_field = models.CharField(max_length=31, db_index=True) + + class Meta: + apps = new_apps + + class AuthorTextFieldWithIndex(models.Model): text_field = models.TextField(db_index=True) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index ef4b192a36..293ae8e546 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -29,11 +29,11 @@ from .fields import ( CustomManyToManyField, InheritedManyToManyField, MediumBlobField, ) from .models import ( - Author, AuthorTextFieldWithIndex, AuthorWithDefaultHeight, - AuthorWithEvenLongerName, AuthorWithIndexedName, Book, BookForeignObj, - BookWeak, BookWithLongName, BookWithO2O, BookWithoutAuthor, BookWithSlug, - IntegerPK, Node, Note, NoteRename, Tag, TagIndexed, TagM2MTest, - TagUniqueRename, Thing, UniqueTest, new_apps, + Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex, + AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName, + Book, BookForeignObj, BookWeak, BookWithLongName, BookWithO2O, + BookWithoutAuthor, BookWithSlug, IntegerPK, Node, Note, NoteRename, Tag, + TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps, ) @@ -49,9 +49,10 @@ class SchemaTests(TransactionTestCase): available_apps = [] models = [ - Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, - BookWeak, BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Node, - Note, Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, + Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex, + AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookWeak, + BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Node, Note, + Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, ] # Utility functions @@ -223,6 +224,49 @@ class SchemaTests(TransactionTestCase): self.fail("No FK constraint for author_id found") @skipUnlessDBFeature('supports_foreign_keys') + def test_char_field_with_db_index_to_fk(self): + # Create the table + with connection.schema_editor() as editor: + editor.create_model(Author) + editor.create_model(AuthorCharFieldWithIndex) + # Change CharField to FK + old_field = AuthorCharFieldWithIndex._meta.get_field('char_field') + new_field = ForeignKey(Author, CASCADE, blank=True) + new_field.set_attributes_from_name('char_field') + with connection.schema_editor() as editor: + editor.alter_field(AuthorCharFieldWithIndex, old_field, new_field, strict=True) + # The new FK constraint is present + constraints = self.get_constraints(AuthorCharFieldWithIndex._meta.db_table) + constraint_fk = None + for name, details in constraints.items(): + if details['columns'] == ['char_field_id'] and details['foreign_key']: + constraint_fk = details['foreign_key'] + break + self.assertEqual(constraint_fk, ('schema_author', 'id')) + + @skipUnlessDBFeature('supports_foreign_keys') + @skipUnlessDBFeature('supports_index_on_text_field') + def test_text_field_with_db_index_to_fk(self): + # Create the table + with connection.schema_editor() as editor: + editor.create_model(Author) + editor.create_model(AuthorTextFieldWithIndex) + # Change TextField to FK + old_field = AuthorTextFieldWithIndex._meta.get_field('text_field') + new_field = ForeignKey(Author, CASCADE, blank=True) + new_field.set_attributes_from_name('text_field') + with connection.schema_editor() as editor: + editor.alter_field(AuthorTextFieldWithIndex, old_field, new_field, strict=True) + # The new FK constraint is present + constraints = self.get_constraints(AuthorTextFieldWithIndex._meta.db_table) + constraint_fk = None + for name, details in constraints.items(): + if details['columns'] == ['text_field_id'] and details['foreign_key']: + constraint_fk = details['foreign_key'] + break + self.assertEqual(constraint_fk, ('schema_author', 'id')) + + @skipUnlessDBFeature('supports_foreign_keys') def test_fk_to_proxy(self): "Creating a FK to a proxy model creates database constraints." class AuthorProxy(Author): |
