[英]django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id
[英]django.db.utils.IntegrityError: NOT NULL constraint failed: new__inventory_app_item.accounting_class_id
所以我從事 Django 項目已經有一段時間了,我即將完成它。 我決定為其添加一個功能,並在我的 model 中添加了一個新的外鍵。 我做了makemigrations my_app
,但是當我嘗試遷移它時,出現了這個錯誤:
django.db.utils.IntegrityError: NOT NULL constraint failed: new__inventory_app_item.accounting_class_id
這是 model:
class AccountingClass(models.Model):
name = models.CharField(max_length=150, default="", blank=False)
def __str__(self):
return self.name
這是它的外鍵引用:
`accounting_class = models.ForeignKey("AccountingClass", on_delete=models.SET_NULL, default="", blank=False, null=True, help_text="The accounting class of the object")`
這有什么問題?
這意味着您的數據庫中有數據,其中會計 class 參考沒有 PK 類型的值。 我懷疑這是因為您設置了default=""
這是一個字符串(varchar)。 這可以完全刪除,因為您有null=True
所以默認情況下,當沒有參考會計 class 時,您將擁有null
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.