1

I had some mess with migrations in production and localy. Finally the situation was that in production there were only initial migration and localy there were 8 migrations or something. So I decided to use

python manage.py migrate app zero 

both in production and localy(django 1.8.7). In prodcution it worked but locally it raised error which didn't appear before after makemigrations or migrate command.

django.db.utils.IntegrityError: NOT NULL constraint failed: app_userprofile__new.phone_number

after few attempts to try different things, the error began to appear after migrate commands too.

The model itself :

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    phone_number = models.IntegerField(null=True, blank=True, default=None)

1 Answer 1

3

Check your local database.

This error usually happens when one or more records do not meet the NOT NULL requirement

    UserProfile.objects.filter(phone_number=None)

You can resolve this by filling the phone_number field of the objects found

Or deleting objects that do not have filled phone_number

UPDATE

Manage database changes with database migrations can prevent this type of situation

Sign up to request clarification or add additional context in comments.

6 Comments

yeah, deleting some very old records helped the situation. could you please also say whether what I am doing with migrations issue is correct? or at least not harmful?
You have not performed incorrect procedures from my point of view. Old data can cause this type of error when new constraints are established.
why does it think the field is NOT NULL? blank=True should take care of this, no? Is there something I can say in the model definition to take care of this?
@sureshvv in the case (or when it occurs to me) is because a field that was not initially mandatory, happens to be after some change. If it is NOT NULL from the beginning it will not go through this.
database migrations can prevent this type of situation as well
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.