Django.db.utils.IntegrityError: NOT NULL constraint failed: sales_position.created

Hi I'm working on a django project where I'm parsing a CSV file to create objects. I'm getting a database error that results in

django.db.utils.IntegrityError: NOT NULL constraint failed: sales_position.created

It looks like a database error because before the traceback I get the follwing message:

sqlite3.IntegrityError: NOT NULL constraint failed: sales_position.created

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

Does deleting all migrations except 0001_initial.py fix this?

Delete your all migration folders.

And try these 3 commands:

python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001 # you will get this value after makemigrations.
python manage.py migrate

You are trying to assign null value to not nullable field created in your Position model, to make your code work either ensure that data in your CSV file has created with some value or add null=True in your Position model created field

class Position(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()
    price = models.FloatField(blank=True)
    created = models.DateTimeField(blank=True, null=True)

After that you should run python manage.py makemgirations and python manage.py migrate

Back to Top