Django Not Populating AutoField

1,504 views
Skip to first unread message

Lee Hughes

unread,
Aug 18, 2011, 7:54:28 PM8/18/11
to django-users
In my model I have

id = models.AutoField(primary_key=True)

but upon saving a new record I get 

null value in column "id" violates not-null constraint

I suspect this may be caused by my overriding save_model in admin.py to populate timestamp fields:

  def save_model(self, request, obj, form, change):
    if change:
      obj.updated_by = request.user.username
    else:
      obj.created_by = request.user.username
    obj.save()

Any ideas on how to restore the AutoField functionality?

Thanks-

--
Lee

Shawn Milochik

unread,
Aug 18, 2011, 8:32:46 PM8/18/11
to django...@googlegroups.com
As defined your id field doesn't differ from Django's default. Just get
rid of your custom id field.


Lee

unread,
Aug 19, 2011, 1:13:16 PM8/19/11
to Django users
<<Just get rid of your custom id field.>>

Did so -- no effect. I even disabled my overridden save_model so I'm
back to out-of-the-box Admin setup -- no effect. This is with Postgres
and the psycopg2 driver. Is no one else seeing this problem?

Landy Chapman

unread,
Aug 19, 2011, 1:47:25 PM8/19/11
to Django users
Did you create the table using django?
Can you post the table definition in postgresql?

If you still have an empty database, you could try dropping the id
column and do a django dbsync

otherwise you may have to create a sequence and apply it to the id
field. Something along the lines of:

create sequence id_seq start 1 increment 1;
alter table xxx alter column id set default nextval('id_seq');

Lee

unread,
Aug 19, 2011, 2:05:24 PM8/19/11
to Django users
Landy - thanks for that idea. Am I correct in understanding that
setting a field to AutoField has no effect on save_model, and simply
causes dbsync to add the sequence/nextval to the Postgres table? If so
that is my problem, because I want to have one database schema that
works across different DBMSes, and the sequence/nextval syntax is
Postgres-specific so I have not been using dbsync.

I was hoping AutoField caused the *application* to manage the auto-
incrementing primary key, not the database -- is there an easy way to
do this in Django/Admin?

Thanks very much for the help.

Lee

Landy Chapman

unread,
Aug 19, 2011, 3:46:40 PM8/19/11
to Django users


On Aug 19, 2:05 pm, Lee <lhughe...@gmail.com> wrote:
> Landy - thanks for that idea. Am I correct in understanding that
> setting a field to AutoField has no effect on save_model, and simply
> causes dbsync to add the sequence/nextval to the Postgres table?

I am not sure; On a droid phone so I can't look at the source code or
run tests.

> If so
> that is my problem, because I want to have one database schema that
> works across different DBMSes, and the sequence/nextval syntax is
> Postgres-specific so I have not been using dbsync.

Django abstracts this away.. If django is creating your tables, you
can trust it will do The Right Thing(tm) for whatever DBMS you are
using. If you're not the trusting type you can run this command to
find out what django would do to create your tables:
django-admin.py sqlall

Then you can change the database_backend in settings.py to find out
what the differences would be.

> I was hoping AutoField caused the *application* to manage the auto-
> incrementing primary key, not the database -- is there an easy way to
> do this in Django/Admin?
That is almost never a good idea (*maybe* there is an edge case I've
never seen). It isn't easy to prevent collisions (two records with
same id). Imagine two users adding new records at the same time. Why
do that when the problem is already solved by your DBMS?

> Thanks very much for the help.
You're quite welcome!
Reply all
Reply to author
Forward
0 new messages