简体   繁体   中英

IntegrityError Primary Key Invalid

I am following sentdex's tutorials for django web development, however I have hit a wall - the error shows:

django.db.utils.IntegrityError: The row in table 'main_tutorial' with primary key '1' has an invalid foreign key: main_tutorial.tutorial_series_id contains a value '1' that does not have a corresponding value in main_tutorialseries.id.

I've tried removing default=1 , and on_delete=models.SET_DEFAULT/CASCADE .

I've also tried deleting the SQL database, migrations. I've tried using SQ Browser for SQlite to try and change things.

Sadly I've spent hours staring at this and trying to find an answer, any help would be greatly appreciated. Thanks! The code is :

from django.db import models
from datetime import datetime


class TutorialCategory(models.Model):

    tutorial_category = models.CharField(max_length=200)
    category_summary = models.CharField(max_length=200)
    category_slug = models.CharField(max_length=200, default=1)

    class Meta:
        # Gives the proper plural name for admin
        verbose_name_plural = "Categories"

    def __str__(self):
        return self.tutorial_category

class TutorialSeries(models.Model):
    tutorial_series = models.CharField(max_length=200, default=1)
    tutorial_category = models.ForeignKey(TutorialCategory, default=1,
                                          verbose_name="Category", on_delete=models.SET_DEFAULT)
    series_summary = models.CharField(max_length=200)

    class Meta:
        # otherwise we get "Tutorial Seriess in admin"
        verbose_name_plural = "Series"

    def __str__(self):
        return self.tutorial_series


class Tutorial(models.Model):
    tutorial_title = models.CharField(max_length=200)
    tutorial_content = models.TextField()
    tutorial_published = models.DateTimeField('date published')

    tutorial_series = models.ForeignKey(TutorialSeries, default=1, verbose_name="Series", on_delete=models.SET_DEFAULT)
    tutorial_slug = models.CharField(max_length=200)
    def __str__(self):
        return self.tutorial_title

Any details to help walk me through this obstacle would be greatly appreciated. Thanks!

remove the default=1 and change models.SET_DEFAULT to models.CASCADE in your fields tutorial_series and tutorial_category

tutorial_category = models.ForeignKey(TutorialCategory, verbose_name="Category", on_delete=models.CASCADE)

and

tutorial_series = models.ForeignKey(TutorialSeries , verbose_name="Series", on_delete=models.CASCADE)

The first row in Tutorial is associated (via an FK) to a row in TutorialSeries which doesn't exist. ie Bad Data. You've got some options as to how you want to fix this:

  1. Perhaps you're getting this error because there's no data in TutorialSeries. So one option is to delete the row with the ID of 1 on the table Tutorial. This is an easy solution.

  2. Perhaps you deleted the FK row in TutorialSeries, in which case, add it back. Just make sure its ID is 1, so the PK will be associated to it. This might be an easy option too, especially if there's no other data in TutorialSeries.

  3. Perhaps there is data in TutorialSeries, just not any with the corresponding FK row ID. So another option is to point the PK to another row in TutorialSeries. In others words, edit the bad data.

  4. Delete the migration. Change the FK default, and redo makemigration and migrate.

remove the default=1 and change models.SET_DEFAULT to models.CASCADE in your fields like sachin mathew mentioned in the comment

then try Resetting your Django database video :- refvideo

I found solution:

  1. Delete in dir migrations file with creating 'tutorial' model or change file with migrations if file create several models at once, drop:
 migrations.CreateModel( name='tutorial', ,,,,)
  1. Delete table 'main_tutorial' in db.sqlite3.

  2. Use "makemigrations" and "migrate"

  3. Profit)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM