簡體   English   中英

FOREIGN KEY約束刪除django失敗

[英]FOREIGN KEY constraint failed delete django

對不起我的英語不好。 我花了2天的時間,但不明白為什么會有錯誤:

FOREIGN KEY constraint failed

我使用python 2.7一切正常,然后我更新到python 3.5.2 ,現在當我嘗試刪除時出現此錯誤。 我有以下模型:

class Auction(models.Model):
    sealers = models.ForeignKey(User, related_name="sealers", on_delete=models.CASCADE)
    buyer = models.ManyToManyField(User, related_name='buyer')
    product_in_auction = models.ForeignKey(Product, related_name='product_in_auction', null=True, blank=True, on_delete=models.CASCADE)
    name_auction = models.CharField(max_length=64, blank=False, null=False)
    description = models.TextField(blank=True, null=True, default=None)
    conf_propose = models.OneToOneField('Propose', null=True, blank=True, related_name='confirm_proposition', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True)

    class Meta:
        verbose_name = 'Auction'
        verbose_name_plural = 'Auctions'

    def __str__(self):
        return "%s" % (self.name_auction)


class Propose(models.Model):
    buyer = models.ForeignKey(User, related_name="propositions_of_auction", on_delete=models.CASCADE, null=True, blank=True)
    auction = models.ForeignKey(Auction, related_name="propositions_of_auction", on_delete=models.CASCADE)
    bit = models.DecimalField(max_digits=10, decimal_places=2, default=0, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True)

    class Meta:
        verbose_name = 'Proposition of the auction'
        verbose_name_plural = 'Propositions of the auction'

    def __str__(self):
        return "%s" % (self.bit)

當我嘗試刪除一些auction會時出現錯誤

FOREIGN KEY constraint failed

我認為問題不在於python版本。 就像錯誤說的那樣,您具有外鍵約束。 因此,在Propose模型中,有一個名為Auction的字段,這是模型Auction的ForeignKey。因此,如果要刪除某些拍賣實例,它也會影響Propose表中的數據。

每次我想使用管理來更改某些內容時,都會遇到此錯誤。 我很幸運沒有大型數據庫。 因此,我只是刪除了所有遷移__init__.py除外),數據庫,然后使用manage.py makemigrationsmanage.py migrate manage.py makemigrations再次進行manage.py migrate 這種簡單的方法對我有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM