diff options
Diffstat (limited to 'tests/schema/tests.py')
| -rw-r--r-- | tests/schema/tests.py | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index a765528d50..7276912c71 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -14,7 +14,8 @@ from django.db.models.deletion import CASCADE, PROTECT from django.db.models.fields import ( AutoField, BigAutoField, BigIntegerField, BinaryField, BooleanField, CharField, DateField, DateTimeField, IntegerField, PositiveIntegerField, - SlugField, TextField, TimeField, UUIDField, + SlugField, SmallAutoField, SmallIntegerField, TextField, TimeField, + UUIDField, ) from django.db.models.fields.related import ( ForeignKey, ForeignObject, ManyToManyField, OneToOneField, @@ -1179,6 +1180,28 @@ class SchemaTests(TransactionTestCase): # Fail on PostgreSQL if sequence is missing an owner. self.assertIsNotNone(Author.objects.create(name='Bar')) + def test_alter_autofield_pk_to_smallautofield_pk_sequence_owner(self): + """ + Converting an implicit PK to SmallAutoField(primary_key=True) should + keep a sequence owner on PostgreSQL. + """ + with connection.schema_editor() as editor: + editor.create_model(Author) + old_field = Author._meta.get_field('id') + new_field = SmallAutoField(primary_key=True) + new_field.set_attributes_from_name('id') + new_field.model = Author + with connection.schema_editor() as editor: + editor.alter_field(Author, old_field, new_field, strict=True) + + Author.objects.create(name='Foo', pk=1) + with connection.cursor() as cursor: + sequence_reset_sqls = connection.ops.sequence_reset_sql(no_style(), [Author]) + if sequence_reset_sqls: + cursor.execute(sequence_reset_sqls[0]) + # Fail on PostgreSQL if sequence is missing an owner. + self.assertIsNotNone(Author.objects.create(name='Bar')) + def test_alter_int_pk_to_autofield_pk(self): """ Should be able to rename an IntegerField(primary_key=True) to @@ -1211,6 +1234,28 @@ class SchemaTests(TransactionTestCase): with connection.schema_editor() as editor: editor.alter_field(IntegerPK, old_field, new_field, strict=True) + @isolate_apps('schema') + def test_alter_smallint_pk_to_smallautofield_pk(self): + """ + Should be able to rename an SmallIntegerField(primary_key=True) to + SmallAutoField(primary_key=True). + """ + class SmallIntegerPK(Model): + i = SmallIntegerField(primary_key=True) + + class Meta: + app_label = 'schema' + + with connection.schema_editor() as editor: + editor.create_model(SmallIntegerPK) + self.isolated_local_models = [SmallIntegerPK] + old_field = SmallIntegerPK._meta.get_field('i') + new_field = SmallAutoField(primary_key=True) + new_field.model = SmallIntegerPK + new_field.set_attributes_from_name('i') + with connection.schema_editor() as editor: + editor.alter_field(SmallIntegerPK, old_field, new_field, strict=True) + def test_alter_int_pk_to_int_unique(self): """ Should be able to rename an IntegerField(primary_key=True) to |
