Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 raise IntegrityError(
django.db.utils.IntegrityError: The row in table 'jobs_jobs' with primary key '1' has an invalid foreign key: jobs_jobs.company_id contains a value 'Google' that does not have a corresponding value in jobs_company.id.


This is the whole error.

I got this when I ran
python manage.py migrate


I am not sure why I am getting this error. I don't have the value 'Google' in the company models.py

Models.py:

class Company(models.Model): #here we telling about the company,all its details
    title = models.CharField(max_length=200, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    uniqueId = models.CharField(null=True, blank=True, max_length=100 )
    companyLogo = models.ImageField(default = 'default.png', upload_to = 'upload_imges')
    slug = models.SlugField(max_length=500, unique=True, null=True, blank=True)
    seoDescription = models.CharField(max_length=500, null=True, blank=True)
    seoKeywords = models.CharField(max_length=500, null=True, blank=True)
    
    def __str__(self):
        return '{} - {}'.format(self.title, self.uniqueId)
    
    def get_absolute_url(self):
        return reverse('company-detail', kwargs = {'slug': self.slug }) 
    
    def save(self, *args, **kwargs):
        if self.uniqueId is None:
            self.uniqueId = str(uuid4()).split('-')[0]
            self.slug = slugify('Company {} {}'.format(self.title, self.uniqueId))
            
        self.slug = slugify('Category {} {}'.format(self.title, self.uniqueId))
        self.seoDescription = 'Apply for {} Jobs online, start your career journey today'.format(self.title)
        self.seoKeywords = '{}, Jobs,  Careers Portal, Apply Jobs'.format(self.title)
        super(Company, self).save(*args, **kwargs)



class Jobs(models.Model):

    title = models.CharField(max_length=200, null=True, blank=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True, blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)
    location = models.CharField(max_length=200)
    salary = models.CharField(max_length=100)
    uniqueId = models.CharField(null=True, blank=True, max_length=100 )
    type = models.CharField(max_length=10,choices =TYPE_CHOICES, default=FULL_TIME,null=True, blank=True,)
    experience = models.CharField(max_length=10,choices = EXP_CHOICES, default = TIER1)
    summary = models.TextField(null=True, blank=True) 
    description = models.TextField(null=True, blank=True) 
    requirements = models.TextField(null=True, blank=True)
    duties = models.TextField(null=True, blank=True)
    enquires = models.TextField(null=True, blank=True)
    applications =models.TextField(null=True, blank=True)
    note = models.TextField(null=True, blank=True)
    closing_date = models.DateField(null=True, blank=True)
    #logo = models.ImageField(default = 'default.png', upload_to = 'upload_imges')
    date_posted = models.DateTimeField(default=timezone.now)
    owner = models.ForeignKey(User, on_delete=models.CASCADE) 
    contract_type = models.CharField(max_length=100, null= True, blank=True)
    slug = models.SlugField(max_length=500, unique=True, blank=True, null=True)
    seoDescription = models.CharField(max_length=500, null=True, blank=True)
    seoKeywords = models.CharField(max_length=500, null=True, blank=True)
    urlLink = models.CharField(max_length=500, null=True, blank=True) 
    
    
    
    def _str_(self):
       return '{} - {} - {}'.format(self.company, self.title, self.location) 
    
    def get_absolute_url(self):
        return reverse('job-detail', kwargs={'slug': self.slug})
    
    def save(self, *args, **kwargs):
        
        if self.uniqueId is None:
            self.uniqueId = str(uuid4()).split('-')[0]
            self.slug = slugify('{} {} {} {}'.format('{} {} {}'.format(self.company, self.title, self.location, self.uniqueId)))
            
        self.slug = slugify('{} {} {} {}'.format('{} {} {}'.format(self.company, self.title, self.location, self.uniqueId)))
        self.seoKeywords = 'Careers Portal, Online job application, full time jobs, part time jobs, get a job, apply for a job, {}, {}'.format(self.company.title, self.title)
        self.seoDescription = '{}'.format('Careers Portal {} Jo application. Apply for job: {} in {}, online today'.format(self.company.title, self.title, self.location))
        super(Jobs, self).save(*args, **kwargs)


What I have tried:

I can not find a definitive solution to this problem which can help
Posted
Updated 1-Aug-22 18:54pm
v2
Comments
Richard MacCutchan 2-Aug-22 4:38am    
That sounds like something has been corrupted in your models, or the migrations file. Try deleting the contents of your migrations directory and rerunning "makemigrations".
Kavya Bhargava 2-Aug-22 15:02pm    
Hey so I deleted the migrations folder from the jobs app(which had company,job,category model). Then I ran make migrations and migrate, it gave no error. But still I did not got my migrations folder back. its not recognizing the jobs model i used in HTML
Richard MacCutchan 3-Aug-22 3:30am    
I said delete the "contents" not the folder. You need the folder to exist before you run the migrations.
Kavya Bhargava 2-Aug-22 15:04pm    
I have the original migrations folder in GitHub so I can put it back. if you think deleting was a bad idea

1 solution

The error is saying that the database can't store that information because there is a problem with a foreign key field.

A Foreign Key is a field that establishes a relationship between two tables: the value will be the same in both tables. For example, an Invoices table will have a InvoiceNumber, a Customer, an InvoiceDate, and a Total.
There will be a separate table Customers which has a CustomerID, an InvoiceAddress, and a DeliveryAddress.
And a third table called InvioceLines which has the LineID, the InvoiceID it refers to, the Product details, the quantity purchased, the price per item, and teh tyotal price for the products.

The InvoiceLines.InvoiceID will be a Foreign Key to the Invoices.InvoiceID to establish the relationship, and the Invoces.CustomerID will be a Foreign Key to the Customes.CustomerID to establish that relationship.

In the real world, when you add an Invoice, you have to have an existing Customer to refer to, or the Invoice is not complete - if you don't know where to send the goods, you can't sell them!
Similarly, when you add a product to an Invoice, you have to have an invoice to refer to!

You can't have an Invoice Line that isn't associated directly with a specific Invoice, or an Invoice that isn't associated with a specific Customer.
You can add an remove Invoice lines without affecting the other tables, but you can't remove an Invoice without first removing all it's Invoice Lines or the Invoice lines become "orphaned" - they refer to an non-existent Invoice!

That's what a Foreign Key ensures - the DB system will not let you create "orphan" rows because the table that the Foreign Key row refers to must contain a valid matching row before the Foreign Key row can be added. YOu have to add the rows in the "parent" table before you can create matching rows in the "child"

And that's what the error message is saying: you are creating a Foreign Key row which doesn't exist in the table it refers to.

We can't fix that for you: we have no access to your database to even start working out which columns of which tables are Foreign Keys, and which table(s) they refer to!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900