5

I developed a Django Application and it was working correctly, until I did a data migration to the database created by django migration, I migrated the data using an sql script and Pgadmin.

Now I have the database full with records but when I am trying to add new record using django form I got the below error:

duplicate key value violates unique constraint > "learningcenters_partnerorganization_pkey" DETAIL: Key (id)=(1) already exists.

taking into consideration that the available id for this table is 10.

Model:

class SLPAcademicRound(models.Model):
    name = models.CharField(max_length=45,
                            unique=True,
                            verbose_name=_('Academic Round Name'))
    code = models.CharField(max_length=5,
                            unique=True,
                            verbose_name=_('Code'))
    cycle = models.ForeignKey(
        SLPCycle,
        blank=False, null=True,
        verbose_name=_('SLP Cycle/SLP Cycle'),
        on_delete=models.CASCADE,
    )

    learning_center = models.ForeignKey(
        LearningCenter,
        blank=False, null=True,
        verbose_name=_('Learning Center'),
        on_delete=models.CASCADE,
    )

    round_date_start = models.DateField(
        blank=True,
        null=True,
        verbose_name=_('SLP Round Start Date')
    )
    round_date_end = models.DateField(
        blank=True,
        null=True,
        verbose_name=_('SLP Round End Date')
    )
    current_round = models.BooleanField(
        blank=True,
        null=True,
        verbose_name=_('Current Round')
    )

View:

class AddSLPAcademicRoundView(LoginRequiredMixin,
              GroupRequiredMixin,
              CreateView):

    template_name = 'bootstrap4/common_form.html'
    form_class = SLPAcademicRoundForm
    queryset= SLPAcademicRound.objects.all()
    group_required = ["LearningCenterManager"]


    def get_absolute_url(self):
        return reverse("slp:slp_academic_round_list")


    def form_valid(self, form):
        print((form.cleaned_data))
        form.save(self.request)
        return super(AddSLPAcademicRoundView, self).form_valid(form)

    def get_form_kwargs(self, *args, **kwargs):
        kwargs = super().get_form_kwargs(*args, **kwargs)
        kwargs['user'] = self.request.user
        return kwargs
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Salma
  • 311
  • 1
  • 6

2 Answers2

5

I found the solution by pg_get_serial_sequence can be used to avoid any incorrect assumptions about the sequence. This resets the sequence in one shot:

SELECT pg_catalog.setval(pg_get_serial_sequence('table_name', 'id'), (SELECT MAX(id) FROM table_name)+1);

Salma
  • 311
  • 1
  • 6
0

Run the command

manage.py sqlsequencereset

This will give you a script to reset the table ids to normal. Thereafter, run the script in psql and all the table's sequences will be reset.

Hayden Eastwood
  • 928
  • 2
  • 10
  • 20
issac
  • 1
  • I have tried your suggestion but it seems to reset the id to 1, I want the id to sync with the exited ids in the database. so it will continue the sequence. any suggestion? – Salma Apr 16 '20 at 08:59