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?
Edit:
I have the following 3 classes in sales/models.py
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)
class Sale(models.Model):
transaction_id = models.CharField(max_length=12, blank=True)
positions = models.ManyToManyField(Position)
total_price = models.FloatField(blank=True, null=True)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
salesman = models.ForeignKey(Profile, on_delete=models.CASCADE)
created = models.DateTimeField(blank=True)
updated = models.DateTimeField(auto_now=True)
class CSV(models.Model):
file_name = models.FileField(upload_to='csvs')
activated = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
Edit 2:
imported Product class in sales/models.py
:
class Product(models.Model):
name = models.CharField(max_length=120)
image = models.ImageField(upload_to='products', default='no_picture.png')
price = models.FloatField(help_text='in US dollars $')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
CodePudding user response:
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
CodePudding user response:
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