Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You are trying to add a non-nullable field 'id' to contact_info without a default

I am using the command python manage.py makemigrations

However, I get this error:

You are trying to add a non-nullable field 'id' to contact_info without a default; we can't do that (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)  2) Quit, and let me add a default in models.py 

Here is models.py:

class Document(models.Model):     docfile = models.FileField(upload_to='documents/')   class contact_number(models.Model):     contact_numbers = models.CharField(max_length=13)  class contact_address(models.Model):     address = models.CharField(max_length=100)     city = models.CharField(max_length=50)     state = models.CharField(max_length=50)     zip = models.CharField(max_length=5)  class contact_info(models.Model):     first_name = models.CharField(max_length=50)     last_name = models.CharField(max_length=50)     email = models.EmailField()     contact_numbers = models.ManyToManyField(contact_number)     addresses = models.ManyToManyField(contact_address) 
like image 690
shashank Avatar asked Oct 07 '15 12:10

shashank


People also ask

How do I add a non nullable field in Django?

To fix 'You are trying to add a non-nullable field 'new_field' to userprofile without a default' error with Python Django, we can set a default value for the non-nullable field. to set the default value of new_field to 'DEFAULT VALUE' by calling the CharField constructor with the default argument.

How do you make a field nullable in Django?

null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.


2 Answers

This happens when a different field was marked as primary key with primary_key=True earlier and you are removing that (in case of which django tries to add an id primary key).

That Django is asking for a default value for a primary key seems to be a bug.

To work around this problem, follow these steps:

  1. Supply a random default value when prompted during makemigrations.

  2. Go to the migration file generated (under your_app\migrations\ and delete the default=x,, x being the random value you supplied in step 1.

  3. While you are at the migration file, make sure the order of actions make sense (e.g., remove/alter one primary key before adding another). Save and close.

  4. Migrate as usual.

like image 158
mehmet Avatar answered Oct 07 '22 16:10

mehmet


you could set `default="" and also editable=False.

E.g first_name = models.CharField(max_length=50, default="", editable=False).

Adding id field is unnecessary. Django will add it automatically.

Edit: Deleting the last migration files in your migrations folder and retry again. If it doesn't work, repeat the same process, you will know you have deleted the right file when your "makemigrations" command works.

like image 23
pycod333 Avatar answered Oct 07 '22 15:10

pycod333