It is impossible to add a non-nullable field 'details1' to feature without specifying a default. This is because the database needs something to populate existing rows.

4,992 views
Skip to first unread message

Ebenezer Otchere

unread,
Mar 25, 2023, 9:00:32ā€ÆAM3/25/23
to Django users
Am new in django and have been getting errors in migrations, i need help
when i try to do migrations it keeps telling me this
it is impossible to add a non-nullable field 'details' to feature without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:
Ā 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
Ā 2) Quit and manually define a default value in models.py.
Thank youĀ  in advance
from django.db import models

# Create your models here.


class feature(models.Model) :
Ā  Ā 
Ā  Ā  name = models.CharField(max_length=100)
Ā  Ā  extra = models.CharField(max_length=100)
Ā  Ā  details1 = models.CharField(max_length=500)
Ā  Ā 
class art(models.Model):
Ā  Ā 
Ā  Ā  name = models.CharField(max_length=100)
Ā  Ā  details = models.CharField(max_length=500)

Muhammed Lawal

unread,
Mar 25, 2023, 1:57:35ā€ÆPM3/25/23
to django...@googlegroups.com
Enter 1 then ' ' rather. Sorry šŸ˜“

On Sat, 25 Mar 2023, 5:45 p.m. Muhammed Lawal, <lawalmuh...@gmail.com> wrote:
You are seen that error probably because you modified your models after adding some items to database already. When prompted with the error, enter 2, then ' ' an empty string.Ā 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/06f462e2-3e1d-4a1a-8a97-b183586e5663n%40googlegroups.com.

Muhammed Lawal

unread,
Mar 25, 2023, 1:57:38ā€ÆPM3/25/23
to django...@googlegroups.com
You are seen that error probably because you modified your models after adding some items to database already. When prompted with the error, enter 2, then ' ' an empty string.Ā 

On Sat, 25 Mar 2023, 1:59 p.m. Ebenezer Otchere, <swazym...@gmail.com> wrote:
--

Patricia Medina

unread,
Mar 25, 2023, 1:57:40ā€ÆPM3/25/23
to django...@googlegroups.com
Thats because you have values in the database...
If you don't need the data... eliminate the 001, 002, etc files of the migration folder...(this folder is in each application folder) and applicate the migrations again.

--

Henock Lungele

unread,
Mar 25, 2023, 3:53:02ā€ÆPM3/25/23
to django...@googlegroups.com
That means you have done some changes in your model python file. So you have to drop your data base, delete the migration files, recreate the database with the same name as in your configuration python file and do migrations again. This will work or you can add default parameter with a value after max_length parameter for example default="" this will work too but if you take a look to your sql codes, you have define a default behavior if the user type nothing in the input field.
I'm sorry, i speak a little bit english i'm from a french country.
Yours sincerelyĀ 

swazym...@gmail.com

unread,
Mar 25, 2023, 4:36:46ā€ÆPM3/25/23
to django...@googlegroups.com
Thank you all, I was able to find a way around it
Is the details1 I set null = True and it worked for me

Sent from my iPhone

On 25 Mar 2023, at 7:52 PM, Henock Lungele <hlungel...@gmail.com> wrote:

ļ»æ

Sandip Bhattacharya

unread,
Mar 26, 2023, 4:29:27ā€ÆPM3/26/23
to Django users
So what would you want for existing entries in the table which were added before you changed the model?

If you are ok with them being empty, you should use:
details = models.CharField(max_length=500, default=ā€œā€)

And run migrate again.

If you are ok instead of this column being null, then:
details = models.CharField(max_length=500, null=True, blank=True)

I donā€™t have a huge experience with migrations, so others can correct me if I am wrong, but intuitively I feel that defaults should be captured in code than doing one-off fixes manually while running migrations at the command line.

Thanks,
Ā  Sandip


Erik Manuel Herazo Jimenez

unread,
Mar 27, 2023, 12:00:04ā€ÆPM3/27/23
to django...@googlegroups.com
hello, you must apply the default=None, or default="" feature, so that those fields can be filled.
ex:
details1 = models.CharField(max_length=500, default="")

--
Reply all
Reply to author
Forward
0 new messages