gpt4 book ai didi

python - django.db.utils.IntegrityError :

转载 作者:行者123 更新时间:2023-12-02 09:45:55 30 4
gpt4 key购买 nike

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 'tutorial_series_id' that does not have a corresponding value in main_tutorialseries.id.

出现上述错误,无法迁移

这些是我的模型:

    from django.db import models
from datetime import datetime
#Create your models here.

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)
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 Serie*ss* 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", default = datetime.now())
tutorial_series = models.ForeignKey(TutorialSeries, default=1, verbose_name="Series", on_delete=models.SET_DEFAULT)
tutorial_slug = models.CharField(max_length=200,default=1)

def __str__(self):
return self.tutorial_title

最佳答案

我遇到了同样的问题,只是我也在处理同样的问题。您所要做的就是

just delete "migrations" folder from the "main()" and also the db.sqlite file too.

The error is probably occurring because we have Tutorial already in dbwhich is not linked to TutorialSeries so.., to make it linked to thedb, make the changes above and then again perform commands.

我得到的是:

python manage.py makemigrations

输出:

No changes detected

下一个

python manage.py migrate

输出:

Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK

Make sure while performing these commands you have linked Tutorialwith TutorialSeries. And guys making things done this way makes you earlier data lost from the database. Be careful about that.

祝你有美好的一天,快乐的编码。😁

关于python - django.db.utils.IntegrityError :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374172/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com