提问者:小点点

德扬戈。db。乌提尔斯。IntegrityError:非空约束失败:产品\产品。带图像字段的图像错误


我想将imagefield添加到我的模型中。py并上传到我的media_cdn目录中,但当我迁移到基本型号时。他说了这个错误吗

django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD

cmd的输出

operation.database_forwards(self.app_label, schema_editor, old_state, project_state)

文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\迁移\操作\fields.py",第84行,database_forwards字段中,文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\后端\sqlite3\schema.py",第231行,add_field自._remake_table(模型,create_fields=[field])文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\backend\sqlite3\schema.py",第199行,_remake_tableself.quote_name(模型。_meta.db_table),文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\backend\base\schema.py",第112行,在执行cursor.execute(sql,params)中文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\backend\utils.py",第79行,在执行返回超级(CursorDebugWrapper, Self)。执行(sql, params)文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\后端\utils.py",第64行,在执行self.cursor.execute返回(sql, params)文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\utils.py",第94行,在退出six.reraise(dj_exc_type,dj_exc_value,回溯)文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\utils\six.py",第685行,在重新提高value.with_traceback(tb)文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\backend\utils.py",第64行,在执行返回self.cursor.execute(sql,params)文件"C:\用户\PANDEMIC\Desktop\td10\lib\site-包\django\db\后端\sqlite3\base.py",第337行,在执行返回数据库中。Cursor.execute(自我,查询,参数)django.db.utils.完整性错误:非空约束失败:products_product.image

from django.db import models

# Create your models here.

class Product(models.Model):
    name = models.CharField(max_length=40)
    description = models.TextField(max_length=220, blank=True, default=None)
    image = models.ImageField(upload_to="/products_images/", null=True, blank=True, width_field="width_field", height_field="height_field")
    width_field = models.IntegerField(default=0)
    height_field = models.IntegerField(default=0)
    is_active = models.BooleanField(default=True)
    publish = models.DateField(auto_now=False, auto_now_add=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

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

    class Meta:
        ordering = ["-timestamp"]
        verbose_name = 'Product'
        verbose_name_plural = 'Products'

共3个答案

匿名用户

转到migrations文件夹并手动删除名称类型为000*_lastAction_blah-blah的文件,您可以删除,可能是全部,但0001_initial除外。py文件。在那次跑步之后/管理py make migrations app如果您正在更新,它应该会更新您的数据库。

匿名用户

您运行makemigrations appname了吗?

NULL约束失败

这个错误通常意味着没有提供所需的字段,但是我可以看到您在图像字段中设置了空白=True和null=True属性。

匿名用户

只需删除您的基础,并将迁移作为您的应用程序