django.db.utils.IntegrityError: 这个错误困扰了我好几天

huangapple go评论72阅读模式
英文:

django.db.utils.IntegrityError: Stuck with this error for days

问题

我试图在我的模型中进行迁移,但遇到了这个错误:

django.db.utils.IntegrityError: 表 'posts_article' 中主键为 '1' 的行具有无效的外键:posts_article.author_id 包含一个值 '1',该值在 posts_author.id 中没有对应值。

这是我的模型:

from django.db import models
from django.contrib.auth import get_user_model
from django.utils.timezone import now

User = get_user_model()

# 在此创建您的模型
class Author(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
    profile_picture = models.ImageField()

    def __str__(self):
        return self.user.username
    

class Topic(models.Model):
    title = models.CharField(max_length=20, blank=True, null=True)
    subtitle = models.CharField(max_length=20, blank=True, null=True)
    slug = models.SlugField(blank=True, null=True)
    thumbnail = models.ImageField(blank=True, null=True)

    def __str__(self):
        return self.title

class Article(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)
    overview = models.TextField(null=True)
    content = models.TextField(null=True)
    author = models.ForeignKey(Author, on_delete=models.SET_NULL, blank=True, null=True)
    thumbnail = models.ImageField(blank=True, null=True)
    categories = models.ManyToManyField(Topic, blank=True, null=True)
    #featured = models.BooleanField(None, default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title
    
    class Meta:
        ordering = ['-updated', '-created']

我已经在互联网上搜索并尝试了一些解决方案,但问题依然存在。我是Django的新手,所以请不要对我太苛刻 django.db.utils.IntegrityError: 这个错误困扰了我好几天

英文:

I'm trying to make migrations in my model but stuck with this error:

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

Here's my models:

from django.db import models
from django.contrib.auth import get_user_model
from django.utils.timezone import now

User = get_user_model()

# Create your models here.
class Author(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
    profile_picture = models.ImageField()

    def __str__(self):
        return self.user.username
    

class Topic(models.Model):
    title = models.CharField(max_length=20, blank=True, null=True)
    subtitle = models.CharField(max_length=20, blank=True, null=True)
    slug = models.SlugField(blank=True, null=True)
    thumbnail = models.ImageField(blank=True, null=True)

    def __str__(self):
        return self.title

class Article(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)
    overview = models.TextField(null=True)
    content = models.TextField(null=True)
    author = models.ForeignKey(Author, on_delete=models.SET_NULL, blank=True, null=True)
    thumbnail = models.ImageField(blank=True, null=True)
    categories = models.ManyToManyField(Topic, blank=True, null=True)
    #featured = models.BooleanField(None, default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title
    
    class Meta:
        ordering = ['-updated', '-created']

I've searched the internet and tried a few solutions but it's the same. I am totally out of solutions. I am new to Django so please don't be too hard on me django.db.utils.IntegrityError: 这个错误困扰了我好几天

答案1

得分: 1

这是数据不一致的错误,与您的代码无关。尝试删除所有数据和迁移(我认为您的项目还处于早期开发阶段,不是在生产环境中?)

运行 manage.py flush 来从表中删除所有数据。或者您可以物理删除数据库。然后使用 manage.py makemigrations && manage.py migrate 重新创建所有内容。

英文:

This error in data inconsistency, not in your code. Try delete all your data and migrations (i think, your project in early developmentm, not in production?)

Run manage.py flush for remove all data from tables. Or you can phisically remove you database. Then recreate all with manage.py makemigrations && manage.py migrate.

huangapple
  • 本文由 发表于 2023年8月4日 21:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定