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

**URL:** <https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256>\
**Category:** Deployment\
**Created:** [June 10, 2026, 5:20pm UTC](https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256 "2026-06-10T17:20:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![narayanan-ka](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/narayanan-ka/32/17892_2.png) [@narayanan-ka](https://forum.djangoproject.com/u/narayanan-ka)\
**Post date:** [June 10, 2026, 5:20pm UTC](https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256/1 "2026-06-10T17:20:46Z")

</div>

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

This is my PostImage model:

```auto
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:

```auto

 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.

> <https://stackoverflow.com/questions/79954546/null-value-in-column-width-violates-not-null-constraint-while-uploading-image>

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [June 12, 2026, 6:58am UTC](https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256/2 "2026-06-12T06:58:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [June 12, 2026, 7:08am UTC](https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256/3 "2026-06-12T07:08:24Z")

</div>

> [@narayanan-ka](#):
>
> ```auto
> 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)
> 
> ```

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!

---

<div class="post-metadata">

**Author:** ![narayanan-ka](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/narayanan-ka/32/17892_2.png) [@narayanan-ka](https://forum.djangoproject.com/u/narayanan-ka)\
**Post date:** [June 13, 2026, 4:58am UTC](https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256/4 "2026-06-13T04:58:18Z")

</div>

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

> <https://github.com/matthewwithanm/django-imagekit/issues/592#issuecomment-4686872461>
>
> After upgrading Django to 5.2.12 and django-imagekit to 6.1.0,
> 
> I'm getting the …following error while create a PostImage object, I get thrown the error.
> 
> l --\>
> 
> \`\`\`
> 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).
> 
> \`\`\`
> 
> 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="")
> 
> 
> \`\`\`
> 
> 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.
> 
> I've tried resetting the database and starting afresh. But the error persists. Is this error on my end or something to do with django-imagekit ?

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [June 15, 2026, 6:23am UTC](https://forum.djangoproject.com/t/null-value-in-column-width-violates-not-null-constraint-while-uploading-image-to-django/45256/5 "2026-06-15T06:23:19Z")

</div>

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.
