Python django.db.utils.IntegrityError: NOT NULL 约束失败:products_product.image ERROR WITH IMAGE FIELD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42733221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD
提问by Kronvid Vladimir
I wanna add imagefield to my models.py
and upload in my media_cdn directory
but when i migrate to base my model.py he give this error
我想将 imagefield 添加到我的models.py
并上传到我的 media_cdn 目录中,但是当我迁移到我的 model.py 的基础时,他给出了这个错误
django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD
Output from cmd
来自 cmd 的输出
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards field, File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field self._remake_table(model, create_fields=[field]) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\sqlite3\schema.py", line 199, in _remake_table self.quote_name(model._meta.db_table), File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute cursor.execute(sql, params) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\utils.py", line 94, in exitsix.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\utils\six.py", line 685, in reraise raise value.with_traceback(tb) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params) File "C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
文件“C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\migrations\operations\fields.py”,第 84 行,在 database_forwards 字段中,文件“C:\Users\PANDEMIC\Desktop\ td10\lib\site-packages\django\db\backends\sqlite3\schema.py”,第 231 行,在 add_field self._remake_table(model, create_fields=[field]) 文件“C:\Users\PANDEMIC\Desktop\td10 \lib\site-packages\django\db\backends\sqlite3\schema.py”,第 199 行,在 _remake_table self.quote_name(model._meta.db_table),文件“C:\Users\PANDEMIC\Desktop\td10\lib \site-packages\django\db\backends\base\schema.py”,第 112 行,在执行 cursor.execute(sql, params) 文件“C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\ django\db\backends\utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql,params) 文件“C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\utils.py”,第 64 行,在执行中返回 self.cursor.execute(sql, params) 文件“ C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\utils.py”,第 94 行,在exitSix.reraise(dj_exc_type, dj_exc_value, traceback) 文件“C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\utils\six.py”,第685行,在reraise raise value.with_traceback(tb ) 文件“C:\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\utils.py”,第 64 行,在执行时返回 self.cursor.execute(sql, params) 文件“C :\Users\PANDEMIC\Desktop\td10\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute return Database.Cursor.execute(self, query, params) django.db .utils.IntegrityError:NOT NULL 约束失败: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'
采纳答案by Kronvid Vladimir
need just delete your base and make migrations your app
只需删除您的基础并迁移您的应用程序
回答by Nomad Owl
Go to the migrations folder and delete manually files that have 000*_lastAction_blah-blah type of name, you can delete, probably all, but 0001_initial.py file. After that run ./manage.py make migrations app_you_are_updateing, it should update your database.
转到迁移文件夹并手动删除名称为 000*_lastAction_blah-blah 类型的文件,您可以删除,可能所有,但 0001_initial.py 文件。之后运行 ./manage.py make migrations app_you_are_updateing,它应该更新你的数据库。
回答by Gerry Hernandez
Have you ran makemigrations appnameyet?
你运行makemigrations appname了吗?
NOT NULL constraint failed
NOT NULL 约束失败
This error usually means that a field that is required was not provided, but I can see that you have set the blank=True and null=Trueattributes in your image field.
此错误通常意味着未提供必填字段,但我可以看到您已在图像字段中设置了blank=True 和 null=True属性。
回答by Tejas Tank
Similar issues we face too, I locally checked with removing blank=true,null=trueworks, but in production server it did not works well.
我们也面临类似的问题,我在本地检查了删除blank=true,null=true 是否有效,但在生产服务器中它运行不佳。
Than files inside app where issue raise, their a migration folder, I had removed all files and then
比应用程序中出现问题的文件,它们的迁移文件夹,我删除了所有文件,然后
python manage.py makemigrations
and
和
python manage.py migration
Both worked and also runserver works well too.
两者都有效,而且 runserver 也运行良好。
回答by Tejas Tank
Go to migrations folder of your project and delete the migration file created because of the command: python manage.py makemigrations
and re run the same command and then run:
转到项目的迁移文件夹并删除由于该原因创建的迁移文件command: python manage.py makemigrations
并重新运行相同的命令,然后运行:
python manage.py migrate
回答by dee
If you have added a field to a model (or have modified a field), when you migrate, Django will look up the previous records and check whether they meet the constraints given for the field.
如果你在模型中添加了一个字段(或修改了一个字段),当你迁移时,Django 会查找之前的记录并检查它们是否满足为该字段给出的约束。
In your case, even if you have allowed null = True
for the ImageField, when Django tries to add that column to the database, it finds that value for that field has 'not been defined' for previous records.
在您的情况下,即使您允许null = True
使用 ImageField,当 Django 尝试将该列添加到数据库时,它也会发现该字段的值尚未为以前的记录“定义”。
Solution:you have to add default = None
in the ImageField, which will make the values for previous records NULL
.
解决方案:您必须添加default = None
ImageField,它将为以前的记录创建值NULL
。
Moreover,when Django runs migrations, even if you specify the migration number (for instance, python manage.py migrate your_app_name 00xx
), it checks for dependencies in previous migrations. Now, as the migration you have made here has caused an error, even if you correct it (as given) and try to migrate, it will still cause the same error.
此外,当 Django 运行迁移时,即使您指定迁移编号(例如,python manage.py migrate your_app_name 00xx
),它也会检查先前迁移中的依赖项。现在,由于您在此处进行的迁移导致了错误,即使您更正它(如给定)并尝试迁移,它仍然会导致相同的错误。
Therefore, you need to remove all previous migrations up to the one which caused the error first, and run makemigrations
again. Then you can run migrate
without a problem.
因此,您需要删除所有先前的迁移,直到第一个导致错误的迁移,然后makemigrations
再次运行。然后你可以migrate
毫无问题地运行。
回答by siddhant
Basically if you have made changes in your model class , you need to delete all the objects created before , since they have old attributes.
基本上,如果您对模型类进行了更改,则需要删除之前创建的所有对象,因为它们具有旧属性。