Legacy DB: ID is null when .save or .create

31 views
Skip to first unread message

Mei B

unread,
May 17, 2019, 4:12:25 PM5/17/19
to Django users
I'm using a legacy DB with postgres. In one of my models:

class case(models.Model):
     id = models.AutoField(primary_key=True)
     case_id = models.ForeignKey(.....)
     user_id = models.ForeignKey(...)

When I'm saving or creating a new case object, I always get "null value in column 'id' violates not-null"

How do I get this to auto-increment?

Sanjeev Singh

unread,
May 17, 2019, 4:28:52 PM5/17/19
to django...@googlegroups.com
Hey, in django there is a `id` column by default get created for every object you create which will auto-increment so need to create here. In your case your model should be look like this:

class Case(models.Model):
     case_id = models.ForeignKey(.....)
     user_id = models.ForeignKey(...)

And suppose to get the model of id=1:

Case.objects.get(id=1)

thanks,
Sanjeev

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d621db9-39e8-44c8-92e2-80fdabc760af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Pava

unread,
May 17, 2019, 4:33:31 PM5/17/19
to django...@googlegroups.com

You may need to reset your auto increment counter in PostgreSQL. I remember having this problem once.

https://stackoverflow.com/questions/5342440/reset-auto-increment-counter-in-postgres

--

Reply all
Reply to author
Forward
0 new messages