Home > OS >  IntegrityError at /api/course/ (1048, "Column 'category_id' cannot be null")
IntegrityError at /api/course/ (1048, "Column 'category_id' cannot be null")

Time:12-04

Hello everyone i need some help over this issue in Django *IntegrityError at /api/course/ (1048, "Column 'category_id' cannot be null") i got this when i tried to insert a new course *


class Course(models.Model):

category = models.ForeignKey(CourseCategory, on_delete=models.CASCADE)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE , related_name='teacher_courses')
title = models.CharField(max_length=150)
description = models.TextField()  
featured_img = models.ImageField(upload_to='course_imgs/',null=True)
techs = models.TextField(null=True)

class Meta:
    verbose_name_plural = "3. Courses"   

def related_content(self):
        related_content=Course.objects.filter(techs__icontains=self.techs)
        return serializers.serialize('json',related_content)     

def tech_list(self):
    tech_list = self.techs.split(',')
    return tech_list 

def __str__(self):
    return self.title    

class CourseList(generics.ListCreateAPIView):

queryset = models.Course.objects.all()
serializer_class = CourseSerializer 

def get_queryset(self):
    qs = super().get_queryset()
    if 'result' in self.request.GET:
        limit = int(self.request.GET['result'])
        qs = models.Course.objects.all().order_by('-id')[:limit]  
    
    if 'category' in self.request.GET:
        category = self.request.GET['category']
        qs = models.Course.objects.filter(techs__icontains=category)

    if 'skill_name' in self.request.GET and 'teacher' in self.request.GET:
        skill_name = self.request.GET['skill_name']   
        teacher = self.request.GET['teacher']   
        teacher = models.Teacher.objects.filter(id=teacher).first()
        qs = models.Course.objects.filter(techs__icontains=skill_name,teacher=teacher) 

    return qs 

class CourseSerializer(serializers.ModelSerializer):

class Meta:
    model = models.Course
    fields =['id','title','description','category_id','teacher','featured_img','techs','course_chapters','related_content','tech_list']
    depth=1


I have been searching the solution for hours but i did not get any way to solve the issue and i expect you to help me thank you

CodePudding user response:

an integrity error is a database error, you're trying to enter a Course object without specifying (in your case) which CourseCategory object the Course Object is connected to. Here are the docs. You will have to choose an Existing CourseCategory to link the Course to.

CodePudding user response:

Problem resides in your category field in Course model.

 category = models.ForeignKey(CourseCategory, on_delete=models.CASCADE)

You have not assigned any CourseCategory object to your field. If that was a mistake you should assign a CourseCategory otherwise if you want to opt if CourseCategory can be accepted as null you should change that line to:

 category = models.ForeignKey(CourseCategory, on_delete=models.CASCADE, blank=True, null=True)

Furthermore, you need to change your category_id in your serializer to category. If your serializer fields are not custom, they should match their respective model's fields names.

  • Related