Skip to content

Commit

Permalink
Migration that alters unique nullable field to non-nullable
Browse files Browse the repository at this point in the history
This migration fails
  • Loading branch information
ivissani committed Sep 25, 2020
1 parent b81a024 commit 349e58f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions testapp/migrations/0008_test_alter_nullable_in_unique_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.0.4 on 2020-04-20 14:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('testapp', '0007_test_remove_onetoone_field_part2'),
]

operations = [
migrations.CreateModel(
name='TestAlterNullableInUniqueField',
fields=[
('a', models.CharField(max_length=50, null=True, unique=True)),
],
),
migrations.AlterField(
model_name='testalternullableinuniquefield',
name='a',
field=models.CharField(max_length=50, unique=True),
)
]
8 changes: 8 additions & 0 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ class TestRemoveOneToOneFieldModel(models.Model):
# thats already is removed.
# b = models.OneToOneField('self', on_delete=models.SET_NULL, null=True)
a = models.CharField(max_length=50)


class TestAlterNullableInUniqueField(models.Model):
""" Model used to test a single migration that creates a field with unique=True and null=True and then alters
the field to set null=False. This is a common use case when you want to add a non-nullable unique field to a
pre-existing model. In order to make that work you need to first create the unique field as nullable, then
populate the field for every pre-existing instance, and then alter the field to set it to non-nullaable. """
a = models.CharField(max_length=50, unique=True, null=True)

0 comments on commit 349e58f

Please sign in to comment.