django.db.utils.IntegrityError: NOT NULL constrain

2020-06-03 05:07发布

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

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

Output from 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 exit six.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

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'

7条回答
干净又极端
2楼-- · 2020-06-03 05:22

Similar issues we face too, I locally checked with removing blank=true,null=true works, but in production server it did not works well.

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.

查看更多
何必那么认真
3楼-- · 2020-06-03 05:24

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.

查看更多
smile是对你的礼貌
4楼-- · 2020-06-03 05:33

need just delete your base and make migrations your app

查看更多
来,给爷笑一个
5楼-- · 2020-06-03 05:35

Basically if you have made changes in your model class , you need to delete all the objects created before , since they have old attributes.

查看更多
Viruses.
6楼-- · 2020-06-03 05:39

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.

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.

Solution: you have to add default = None in the ImageField, which will make the values for previous records 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.

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.

查看更多
一夜七次
7楼-- · 2020-06-03 05:43

Have you ran makemigrations appname yet?

NOT NULL constraint failed

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=True attributes in your image field.

查看更多
登录 后发表回答