Gocht
Gocht

Reputation: 10256

What should be the default value for a not Null Unique field

I am adding a field to an existing model:

...
booking_id = models.CharField(null=False, unique=True, max_length=5)
...

Now when I run ./manage makemigrations, ask for a default value. This value is created in a pre_save signal. What should I give as default value in this case?

Obviously if I give a default value, when I run ./manage migrate this raises django.db.utils.IntegrityError

Upvotes: 3

Views: 2312

Answers (1)

Alasdair
Alasdair

Reputation: 308789

Add the field with null=True, and create a migration. Then add a data migration to populate the field. Finally, set null=False and create a final migration to add the NOT NULL constraint.

Upvotes: 7

Related Questions