null value in column "width" violates not null constraint while uploading image to django

I’m on Django==5.2.12 and django-imagekit==6.1.0

This is my PostImage model:

class PostImage(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True, related_name="images")
    image = ProcessedImageField(verbose_name=_("image"), storage=post_image_storage, upload_to=upload_to_post_image_directory, width_field="width", height_field="height", blank=False, null=True, format="JPEG", options={"quality": 100}, processors=[ResizeToFit(width=1024, upscale=False)])
    width = models.PositiveIntegerField(editable=False, null=False, blank=False)
    height = models.PositiveIntegerField(editable=False, null=False, blank=False)
    hash = models.CharField(_("hash"), max_length=64, blank=False, null=True)
    thumbnail = ProcessedImageField(verbose_name=_("thumbnail"), storage=post_image_storage, upload_to=upload_to_post_image_directory, blank=False, null=True, format="JPEG", options={"quality": 50}, processors=[ResizeToFit(width=1024, upscale=False)])
    media = GenericRelation(PostMedia)
    image_view_type = (
        ("regularimage", "regularimage"),
        ("gridimages", "gridimages"),
    viewtype = models.CharField(max_length=24, choices=image_view_type, default="")


While create a PostImage object, I get thrown the error.

null value in column “width” violates not null constraint.

Since I require the width and height fields to be calculated for further processing,

I don’t want to change the fields width and height with null=True. However, just to check what happens after doing it, i did try specifying null=True. The PostImage object does get created but with no values for width and height.

Detailed trace:


 django.db.utils.IntegrityError: null value in column "width" of relation "myapi_posts_postimage" violates not-null constraint
my-api-webserver-0  | DETAIL:  Failing row contains (1, posts/9957dc49-639b-490e-87f5-54cab35c0229/582a8f62-86d5-4c3d-a1..., null, null, 01d2d1ce86aa7007f1cecad2259394163b95987a850e6c2d6a48c322a2dc8df0, posts/9957dc49-639b-490e-87f5-54cab35c0229/33d944aa-ded1-49cb-a9..., regularimage, 1).

Haven’t been able to get a response on stack overflow yet. This is driving me nuts.

Hi @narayanan-ka, can you please share a bit more of context, in example:

  • how are you saving your model?
  • if you use a form, the form declaration/configuration?

I suspect that the issue is that currently nothing is setting your width attribute on your model, but without more elements I cannot be sure.

Also keep in mind that this configuration is dangerous: the image field is nullable, yet height and width are not-nullable without any default. If an image is not provided you are going to have an IntegrityError because the field cannot calculate those attributes without an image!

Hi. Working fine with Django 5.0.14. Here is the discussion on django-imagekit.

Good to hear that it works with a previous version of django, however to better identify the problem I need to see how are you saving your model instance.

You are just posting your model setup and the last frame of error stack: to figure out the real problem at least the full stack-trace is required. There are too many context info missing here.