How to rectify Django Integrity Error while creating a nested serializer?

97 views Asked by At

Got an integrity error as shown in job `


class JobSerializer(ModelSerializer):
    company = CompanySerializer(read_only=True)
    skills = SkillSerializer(many=True, read_only=True)

    class Meta:
        fields = ['title', 'description', 'max_salary', 'min_salary', 'employment_type', 'max_experience',
                  'min_experience', 'company', 'location', 'industry_type', 'skills']
        model = Job


`

Nested Serializer got this error ` Django Version: 4.1.3 Exception Type: IntegrityError Exception Value:
NOT NULL constraint failed: naukriapp_job.company_id Exception Location: D:\naukri\naukrienv\lib\site-packages\django\db\backends\sqlite3\base.py, line 357, in execute Raised during: naukriapp.views.Jobviewset Python Executable: D:\naukri\naukrienv\Scripts\python.exe

` I need to rectify this integrity error

1

There are 1 answers

0
Tejesh Nakka On

Please modify your models where you have used a foreign key.

jobs = models.ForeignKey(Job, default=True, on_delete=models.SET_DEFAULT)

By following the above approach integrity issue will get resolved.