MCPcopy
hub / github.com/django/django / test_alter_int_pk_to_autofield_pk

Method test_alter_int_pk_to_autofield_pk

tests/schema/tests.py:2180–2208  ·  view source on GitHub ↗

Should be able to rename an IntegerField(primary_key=True) to AutoField(primary_key=True).

(self)

Source from the content-addressed store, hash-verified

2178 self.assertIsNotNone(Author.objects.create(name="Bar"))
2179
2180 def test_alter_int_pk_to_autofield_pk(self):
2181 """
2182 Should be able to rename an IntegerField(primary_key=True) to
2183 AutoField(primary_key=True).
2184 """
2185 with connection.schema_editor() as editor:
2186 editor.create_model(IntegerPK)
2187
2188 old_field = IntegerPK._meta.get_field("i")
2189 new_field = AutoField(primary_key=True)
2190 new_field.model = IntegerPK
2191 new_field.set_attributes_from_name("i")
2192
2193 with connection.schema_editor() as editor:
2194 editor.alter_field(IntegerPK, old_field, new_field, strict=True)
2195
2196 # A model representing the updated model.
2197 class IntegerPKToAutoField(Model):
2198 i = AutoField(primary_key=True)
2199 j = IntegerField(unique=True)
2200
2201 class Meta:
2202 app_label = "schema"
2203 apps = new_apps
2204 db_table = IntegerPK._meta.db_table
2205
2206 # An id (i) is generated by the database.
2207 obj = IntegerPKToAutoField.objects.create(j=1)
2208 self.assertIsNotNone(obj.i)
2209
2210 def test_alter_int_pk_to_bigautofield_pk(self):
2211 """

Callers

nothing calls this directly

Calls 7

AutoFieldClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
get_fieldMethod · 0.45
alter_fieldMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected