[Django]-Django: non-nullable field without a default

3👍

Others are right. you should set a default value for that field.
but there is a a trick that you can solve this. but it is not a good way… only if you have no choice.
1. comment all of your table
2. run makemigrations and migrate
3. uncomment your table
4.2. run makemigrations and migrate again

3👍

image1=models.ImageField(upload_to='app/image12',help_text="hi", null=True)

That is, set null= True'in the field.

It happens when you change your model after stored database.

0👍

If you want to make it non nullable without default.
you have to provide value for the same field when you create migration file. thats why it asks for the option. type “1” in terminal while python manage.py makemigrations and then provide a value for the field for previously saved row(if any).

Let me know if this helps.

0👍

I have faced a same issue, in my case:

Focus on the alert:

…non-nullable field ‘comment’ to family…

  • model: Family
  • field: comment

Just add one more attr in the field comment of the model family:

default=''

0👍

Just select option 1. Then set a default.
It will create a migration script with the property preserve_default=False which means the default is used only for the migration. It will not affect the default settings in the database.

https://docs.djangoproject.com/en/4.2/ref/migration-operations/#alterfield

👤Andrii

-1👍

You can add this: default=None or default="something"
or add null=True, blank=True.

Leave a comment