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)
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.
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.
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:
Supply a random default value when prompted during makemigrations.
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.
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.
Migrate as usual.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With