Skip to content

Commit

Permalink
[3.0.x] Fixed #30800 -- Fixed migrations crash when altering a field …
Browse files Browse the repository at this point in the history
…with custom db_type().

Regression in 1378d66.
Backport of 580e644 from master
  • Loading branch information
felixxm committed Sep 25, 2019
1 parent f4b7ace commit 1adf85d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion django/db/backends/postgresql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def _field_indexes_sql(self, model, field):
def _field_data_type(self, field):
if field.is_relation:
return field.rel_db_type(self.connection)
return self.connection.data_types[field.get_internal_type()]
return self.connection.data_types.get(
field.get_internal_type(),
field.db_type(self.connection),
)

def _create_like_index_sql(self, model, field):
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/schema/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,26 @@ def test_alter_char_field_decrease_length(self):
with self.assertRaisesMessage(DataError, msg):
editor.alter_field(Author, old_field, new_field, strict=True)

@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
def test_alter_field_with_custom_db_type(self):
from django.contrib.postgres.fields import ArrayField

class Foo(Model):
field = ArrayField(CharField(max_length=255))

class Meta:
app_label = 'schema'

with connection.schema_editor() as editor:
editor.create_model(Foo)
self.isolated_local_models = [Foo]
old_field = Foo._meta.get_field('field')
new_field = ArrayField(CharField(max_length=16))
new_field.set_attributes_from_name('field')
new_field.model = Foo
with connection.schema_editor() as editor:
editor.alter_field(Foo, old_field, new_field, strict=True)

def test_alter_textfield_to_null(self):
"""
#24307 - Should skip an alter statement on databases with
Expand Down

0 comments on commit 1adf85d

Please sign in to comment.