Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NOT NULL constraint failed when running `migrate`

I changed my models.py file and when running migrate I get this error. The property is a OneToOneField(). I have tried adding null=True but that doesn't seem to fix it. It is also weird that even when I comment out the property and run makemigrations followed by migrate, I still get that exact same error. Is there a way to fix this? My model looks like this:

class Estimator(Employee):
    avg_estimate = models.IntegerField()


class Job(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    estimator = models.OneToOneField(Estimator, null=True)
    address = models.CharField(max_length=100)
    completed = models.BooleanField(default=False)
like image 300
bencunningham Avatar asked May 12 '15 04:05

bencunningham


1 Answers

My guess is that you have created a migration without null=True, that won't migrate, then you created a second migration with null=True.

Running "migrate" will run both migrations in order, so the first one will fail again.

Assuming this is the case, then 1: delete the two most recent files in your migrations folder. (Open them first to confirm that they are creating the migrations as I described before deleting them). 2: run makemigrations again, with null=True in your models.py

This should create the equivalent of the second migration file, without the failing intermediate migration.

like image 96
wobbily_col Avatar answered Nov 03 '22 02:11

wobbily_col