Skip to content

Commit 349e58f

Browse files
committed
Migration that alters unique nullable field to non-nullable
This migration fails
1 parent b81a024 commit 349e58f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 3.0.4 on 2020-04-20 14:59
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('testapp', '0007_test_remove_onetoone_field_part2'),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='TestAlterNullableInUniqueField',
15+
fields=[
16+
('a', models.CharField(max_length=50, null=True, unique=True)),
17+
],
18+
),
19+
migrations.AlterField(
20+
model_name='testalternullableinuniquefield',
21+
name='a',
22+
field=models.CharField(max_length=50, unique=True),
23+
)
24+
]

testapp/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,11 @@ class TestRemoveOneToOneFieldModel(models.Model):
7171
# thats already is removed.
7272
# b = models.OneToOneField('self', on_delete=models.SET_NULL, null=True)
7373
a = models.CharField(max_length=50)
74+
75+
76+
class TestAlterNullableInUniqueField(models.Model):
77+
""" Model used to test a single migration that creates a field with unique=True and null=True and then alters
78+
the field to set null=False. This is a common use case when you want to add a non-nullable unique field to a
79+
pre-existing model. In order to make that work you need to first create the unique field as nullable, then
80+
populate the field for every pre-existing instance, and then alter the field to set it to non-nullaable. """
81+
a = models.CharField(max_length=50, unique=True, null=True)

0 commit comments

Comments
 (0)