Opened 6 years ago

Closed 6 years ago

Last modified 18 months ago

#11978 closed Uncategorized (invalid)

'Duplicate key value violates unique constraint' error while changing inline admin model

Reported by: kozlenko Owned by: nobody
Component: Forms Version: 1.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi.
I have a DB error 'Duplicate key value violates unique constraint' while changing model from admin inline form.

The model 'Primary' related to the 'Secondary' as one-to-one via OneToOneField.
When i'm trying to change the data of 'Secondary' on 'Primary' admin change page, after for mubmission raising error 'current transaction is aborted, commands ignored until end of transaction block'. It's logically that previous query returned error and this error is: 'Duplicate key value violates unique constraint "project_secondary_primary_id_key"'.

Here is the source:

models.py

class Primary(models.Model):

    choice_limits = {'groups__name': 'clients', 'is_staff': False}

    user = models.ForeignKey(User)

    additional = models.ManyToManyField(Additional)


class Secondary(models.Model):

    id = models.CharField(max_length = 64, primary_key = True, unique = True)

    file = models.FileField(upload_to=upload_callback, max_length=256)

    upload_date = models.DateField(auto_now_add=True)

    second = models.OneToOneField(Purchase)

admin.py

class PrimaryAdmin(admin.ModelAdmin):

    """
    methods here
    """

    """
    some definitions here
    """

    inlines = [SecondaryInline]

class SecondaryAdmin(admin.ModelAdmin):

    """
    methods here
    """

    """
    some definitions here
    """

admin.site.register(Primary, PrimaryAdmin) 
admin.site.register(Secondary, SecondaryAdmin)

I've already tried to write Forms and Formsets, but that didn't helped.

Thanks for response.

Change History (3)

comment:1 Changed 6 years ago by kozlenko

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

This error resolved by adding new int primary key - error was because of updating id field.

But it is new error now:

'SecondaryFormFormSet' object has no attribute 'new_objects' at django/contrib/admin/options.py in construct_change_message, line 523

comment:2 Changed 6 years ago by kozlenko

  • Resolution set to invalid
  • Status changed from new to closed

Issue resolved. Problem was in internal Secondary exception.
It catch quietly but breaks form submission.

comment:3 Changed 18 months ago by ivanov.george.bogdan@…

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

Can you please share your solution?

Note: See TracTickets for help on using tickets.
Back to Top