Получил ошибку django.db.utils.IntegrityError
Сайт работал как обычно, сегодня снова запустил его (сайт находится на локальном сервере), добавил модель Category, в модель Product добавил столбец description. Выполнил команду makemigrations, все прошло нормально, но когда я ввел команду migrate, вылезла ошибка django.db.utils.IntegrityError: The row in table 'store_product' with primary key '1' has an invalid foreign key: store_product.category_id contains a value '0' that does not have a corresponding value in store_category.id. Я понимаю, что проблема связана с связями, но не понимаю, в чем именно проблема :/
Вот код:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255, verbose_name = 'Имя категории')
def __str__(self):
return self.name
class Customer(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, null = True, blank = True, related_name="customer")
name = models.CharField(max_length=200, null = True)
email = models.CharField(max_length=200, null = True)
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, on_delete = models.CASCADE)
name = models.CharField(max_length=200, null = True)
price = models.DecimalField(max_digits = 7, decimal_places = 0)
description = models.TextField(verbose_name = 'Описание', null = True)
digital = models.BooleanField(default=False, null = True, blank = False)
image = models.ImageField(null = True, blank = True)
def __str__(self):
return self.name
@property
def imageURL(self):
try:
url = self.image.url
except:
url = ''
return url
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete = models.SET_NULL, blank = True, null = True)
date_ordered = models.DateTimeField(auto_now_add = True)
complete = models.BooleanField(default = False, null = True, blank = False)
transaction_id = models.CharField(max_length = 200, null = True)
def __str__(self):
return str(self.id)
@property
def shipping(self):
shipping = False
orderitems = self.orderitem_set.all()
for i in orderitems:
if i.product.digital == False:
shipping = True
return shipping
@property
def get_cart_total(self):
orderitems = self.orderitem_set.all()
total = sum([item.get_total for item in orderitems])
return total
@property
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete = models.SET_NULL, blank = True, null = True)
order = models.ForeignKey(Order, on_delete = models.SET_NULL, blank = True, null = True)
quantity = models.IntegerField(default = 0, null = True, blank = True)
date_added = models.DateTimeField(auto_now_add = True)
@property
def get_total(self):
total = self.product.price * self.quantity
return total
class ShippingAddress(models.Model):
customer = models.ForeignKey(Customer, on_delete = models.SET_NULL, blank = True, null = True)
order = models.ForeignKey(Order, on_delete = models.SET_NULL, blank = True, null = True)
address = models.CharField(max_length=200, null = True)
city = models.CharField(max_length=200, null = True)
state = models.CharField(max_length=200, null = True)
zipcode = models.CharField(max_length=200, null = True)
date_added = models.DateTimeField(auto_now_add = True)
def __str__(self):
return self.address