所以我從事 Django 專案已經有一段時間了,我即將完成它。我決定為其添加一個功能,并為我的模型添加了一個新的外鍵。我做了makemigrations my_app
,但是當我嘗試遷移它時,出現了這個錯誤:
django.db.utils.IntegrityError: NOT NULL constraint failed: new__inventory_app_item.accounting_class_id
這是模型:
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")`
這有什么問題?
uj5u.com熱心網友回復:
這意味著您的資料庫中有資料,其中會計類參考沒有 PK 型別的值。我懷疑這是由于您設定default=""
了字串(varchar)。這可以完全洗掉,因為null=True
默認情況下您將null
在沒有參考會計類時擁有。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506361.html