# Autofield pk generates null value violation in save?

**URL:** <https://forum.djangoproject.com/t/autofield-pk-generates-null-value-violation-in-save/8021>\
**Category:** Using Django\
**Created:** [May 22, 2021, 8:02pm UTC](https://forum.djangoproject.com/t/autofield-pk-generates-null-value-violation-in-save/8021 "2021-05-22T20:02:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbelew](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rbelew/32/4589_2.png) [@rbelew](https://forum.djangoproject.com/u/rbelew)\
**Post date:** [May 22, 2021, 8:02pm UTC](https://forum.djangoproject.com/t/autofield-pk-generates-null-value-violation-in-save/8021/1 "2021-05-22T20:02:11Z")

</div>

My pattern has been to create an explicit `idx = models.AutoField(primary_key=True)` primary key in my model, then create new objects via `new = ModelObject()`, fill all this objects fields EXCEPT its primary key, then count on the `new.save()` to populate the primary key. But I now get an `null value in column "idx" ` exception when i do this?

FYI, i have confirmed via `python manage.py sqlsequencereset` that the pk counter is correct.

What might i be doing wrong, please?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 23, 2021, 12:24am UTC](https://forum.djangoproject.com/t/autofield-pk-generates-null-value-violation-in-save/8021/2 "2021-05-23T00:24:59Z")

</div>

I cannot recreate the behavior you’re describing from the information you’ve posted here.

There’s some context associated with this that’s missing here. Is the addition of the idx field a new change? Is there some other change to your model that has occurred recently? Can you post the code (view or function) where you’re getting this exception?  
Also, what version of Django and Python are you using? Are there any third-party libraries involved with these models? Is this happening with all your models, or just one?

---

<div class="post-metadata">

**Author:** ![rbelew](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rbelew/32/4589_2.png) [@rbelew](https://forum.djangoproject.com/u/rbelew)\
**Post date:** [May 23, 2021, 5:13pm UTC](https://forum.djangoproject.com/t/autofield-pk-generates-null-value-violation-in-save/8021/3 "2021-05-23T17:13:59Z")

</div>

hey Ken, This is part of the same Python2.7/Django11 project being brought up into Python3/Django3 we’ve discussed elsewhere. If this basic pattern seems reasonable, it sounds like i now have TWO minimal working examples to generate!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 23, 2021, 9:05pm UTC](https://forum.djangoproject.com/t/autofield-pk-generates-null-value-violation-in-save/8021/4 "2021-05-23T21:05:14Z")

</div>

Yes, that basic pattern is quite acceptable. I’d look to see if anything in that model’s class hierarchy is overriding the `save` method, causing some kind of weirdness.
