简体   繁体   English

FOREIGN KEY约束删除django失败

[英]FOREIGN KEY constraint failed delete django

Sorry for my english. 对不起我的英语不好。 I spend 2 days but cant understand why i have error: 我花了2天的时间,但不明白为什么会有错误:

FOREIGN KEY constraint failed

I use python 2.7 everything works fine, then i update to python 3.5.2 and now when i try delete i have this error. 我使用python 2.7一切正常,然后我更新到python 3.5.2 ,现在当我尝试删除时出现此错误。 I have models as below: 我有以下模型:

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)

When i try delete some auction i have error 当我尝试删除一些auction会时出现错误

FOREIGN KEY constraint failed

I don't think the problem is about the python version. 我认为问题不在于python版本。 Just like the error says, you have Foreign key constraint. 就像错误说的那样,您具有外键约束。 So in your Propose model you have a field called auction which is the ForeignKey of model Auction, So if you want to delete some auction instance it will affect the data in the table Propose as well. 因此,在Propose模型中,有一个名为Auction的字段,这是模型Auction的ForeignKey。因此,如果要删除某些拍卖实例,它也会影响Propose表中的数据。

I had this error every time I wanted to change something using administration. 每次我想使用管理来更改某些内容时,都会遇到此错误。 I was lucky not to have a big database. 我很幸运没有大型数据库。 So I simply deleted all migrations (except __init__.py ), the database and made them again using manage.py makemigrations and manage.py migrate . 因此,我只是删除了所有迁移__init__.py除外),数据库,然后使用manage.py makemigrationsmanage.py migrate manage.py makemigrations再次进行manage.py migrate This straightforward method worked for me! 这种简单的方法对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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