diff options
| author | Dolan Antenucci <antenucci.d@gmail.com> | 2019-03-19 13:28:47 -0600 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-04-08 22:24:18 +0200 |
| commit | f944cb3d3bc17a97216f8990ff3bb4bee14b6f6b (patch) | |
| tree | 90108862975d19b0a34ab206a1fbf72359079935 /tests | |
| parent | afc708cf6d047b35db57bd7c55baeffef459d279 (diff) | |
Fixed #30266 -- Kept a sequence owner when altering an AutoField/BigAutoField on PostgreSQL.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/schema/tests.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 45befa4e38..0533b32859 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -4,6 +4,7 @@ import unittest from copy import copy from unittest import mock +from django.core.management.color import no_style from django.db import ( DatabaseError, IntegrityError, OperationalError, connection, ) @@ -1103,6 +1104,28 @@ class SchemaTests(TransactionTestCase): Author.objects.create(name='Foo') Author.objects.create(name='Bar') + def test_alter_autofield_pk_to_bigautofield_pk_sequence_owner(self): + """ + Converting an implicit PK to BigAutoField(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 = BigAutoField(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 |
