Reputation: 5246
I have model called Company
. The model pretty simple as you can see. In project I use postgres database.
I have strange problem when try to create new entry by Django admin.
First I add 5 entry in database by sql request from terminal. They have id from 1 to 5. In database client I see them. Now when I try to add new company by admin it raise next error:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "company_pkey"
DETAIL: Key (id)=(5) already exists.
Can someone say how to fix this problem?
models.py:
class Company(models.Model):
name = models.CharField()
admin.py:
class CompanyAdmin(admin.ModelAdmin):
search_fields = ('name',)
admin.site.register(Company, CompanyAdmin)
Upvotes: 0
Views: 181
Reputation: 29967
You are seeing this problem because PostgreSQL uses a sequence to fill the id
field. Somehow the sequence got out of sync, maybe you assigned an id manually.
You have to reset the sequence used for the primary key in the Company
model. The sqlsequencereset
management command will print out the necessary commands.
$ python manage.py sqlsequencereset my_app
Replace my_app
with the name of the app that contains the Company
model. You should be able to pipe its output to the dbshell
management command.
$ python manage.py sqlsequencereset my_app| python manage.py dbshell
Upvotes: 1