Autofield pk generates null value violation in save?

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 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 sqlsequencereset that the pk counter is correct.

What might i be doing wrong, please?

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?

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!

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.

1 Like