Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 486
Gocht Avatar asked Dec 06 '25 02:12

Gocht


1 Answers

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.

like image 141
Alasdair Avatar answered Dec 09 '25 22:12

Alasdair