1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #28816 -- Prevented silencing data loss when decreasing CharField.max_length on PostgreSQL.

This commit is contained in:
Hasan Ramezani
2019-05-11 17:29:24 +02:00
committed by Mariusz Felisiak
parent 519016e5f2
commit 1378d665a1
2 changed files with 26 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ from django.db.models.fields.related import (
)
from django.db.models.indexes import Index
from django.db.transaction import TransactionManagementError, atomic
from django.db.utils import DataError
from django.test import (
TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
)
@@ -803,6 +804,21 @@ class SchemaTests(TransactionTestCase):
with connection.schema_editor() as editor:
editor.alter_field(Author, old_field, new_field, strict=True)
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
def test_alter_char_field_decrease_length(self):
# Create the table.
with connection.schema_editor() as editor:
editor.create_model(Author)
Author.objects.create(name='x' * 255)
# Change max_length of CharField.
old_field = Author._meta.get_field('name')
new_field = CharField(max_length=254)
new_field.set_attributes_from_name('name')
with connection.schema_editor() as editor:
msg = 'value too long for type character varying(254)'
with self.assertRaisesMessage(DataError, msg):
editor.alter_field(Author, old_field, new_field, strict=True)
def test_alter_textfield_to_null(self):
"""
#24307 - Should skip an alter statement on databases with