0

I have two models (UserAddress and CustomUser) in my models.py, the field user address in CustomUser was a many to many field but I decided to change it to a foreign key field. But when I ran python manage.py make migrations it asked me to choose a choice:

what should I do: You are trying to change the nullable field 'email' on customuser to non-nullable without a default; we can't do that (the database needs something to populate exis ting 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. Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous dat a migration)
  3. Quit, and let me add a default in models.py

Here is my models.py file:

class UserAddress(models.Model):
    city = models.CharField(max_length=100)
    address = models.CharField(max_length=200)
    zip_code = models.CharField(max_length=15, blank=True)

    def __str__(self):
        return str(self.id)


class CustomUser(AbstractUser):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField(unique=True)
    user_address = models.ForeignKey(UserAddress, on_delete=models.CASCADE)
delsa_gh
  • 55
  • 1
  • 6
  • 1
    Try to delete the old migrations and the database – dabdya Jan 10 '22 at 08:36
  • But I have a lot of data, there is no way but deleting? @dabdya – delsa_gh Jan 10 '22 at 08:38
  • Since the model has changed, as it is written in the manage output, "the database needs something to fill in the existing rows." This usually happens during development when the model changes, and in this case it is not a pity to delete the data. Another option is to set the default value in the mail field – dabdya Jan 10 '22 at 08:46
  • 1
    Does this answer your question? [You are trying to add a non-nullable field 'id' to contact\_info without a default](https://stackoverflow.com/questions/32991965/you-are-trying-to-add-a-non-nullable-field-id-to-contact-info-without-a-defaul) – Ankit Tiwari Jan 10 '22 at 10:16

1 Answers1

1
class CustomUser(AbstractUser):
   email = models.EmailField(unique=True ,null =True)
  # your other fields

Run python manage.py makemigrations and python manage.py migrate command. The error will be removed.