Opened 19 months ago

Last modified 15 months ago

#26819 assigned Bug

Using a ArrayField on Meta.unique_together throws "unhashable type: 'list'" on validate_unique method

Reported by: Gleber Diniz Owned by: PREMANAND
Component: Forms Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

It happens on a second save (when there is already data to be validated)

On this line: "if row_data in seen_data:", because row_data contains a list and seen_data is a set (row_data: (1, [1, 1]) / seen_data: set())

Example to reproduce:


class Map(models.Model):
    name = models.CharField('name', max_length=128)

class MapSpot(models.Model):
    map = models.ForeignKey('body.Map', related_name='spots')
    position = ArrayField(models.IntegerField(), size=2)

    class Meta:
        unique_together = [('map', 'position')]


class MapSpotInline(admin.TabularInline):
    model = MapSpot
    extra = 0

class MapAdmin(admin.ModelAdmin):
    inlines = [MapSpotInline]

Change History (9)

comment:1 Changed 19 months ago by Tim Graham

Component: Uncategorizedcontrib.postgres
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 Changed 19 months ago by PREMANAND

Owner: changed from nobody to PREMANAND
Status: newassigned

comment:9 Changed 18 months ago by PREMANAND

This issue occurs as the array item is stored in the lists and there is a mis-match while comparison.

The arrayitem is stored in row_data as list and list are not hashable as the error says.

if row_data in seen_data:

This issue can be fixed by changing this line ie; return a tuple instead of list.

Please review and provide your comments.

comment:10 Changed 18 months ago by Tim Graham

Not sure without looking into the details myself, but it might be better if the validate_unique() method took care of casting as needed since changing the return value of to_python() could be backwards-incompatible.

comment:11 Changed 18 months ago by PREMANAND

Description: modified (diff)

As you mentioned, implemented fix in validate_unique() method. The above code fixes the issue and all the unit tests got passed.

The only problem is Im not sure how to write the test case. It needs to be simulated as if a user creates a form in the html page. Do you have any pointers for testing similar condition?

row_data = tuple(d._get_pk_val() if hasattr(d, '_get_pk_val') else tuple(d)
                                  if type(d) is list else d for d in row_data)
Last edited 18 months ago by PREMANAND (previous) (diff)

comment:12 Changed 18 months ago by Tim Graham

Take a look at tests/postgres_tests/ for the existing tests for ArrayField.

By the way, you inadvertently deleted the ticket's description in your last comment. Can you restore it? Deleting your comment should undo that action if you can't find another way.

comment:14 Changed 18 months ago by Claude Paroz

Description: modified (diff)

Description restored.

comment:15 Changed 15 months ago by Meiyer

This happens only for Inline admin models (only validate_unique() of BaseModelFormSet contains the problematic code).

This is not related to the database and is purely a Python issue. This is not limited to ArrayField; any custom field may be affected. In our case, the phonenumber_field.modelfields.PhoneNumberField is causing the error unhashable type: 'PhoneNumber'. Since Django documentation does not mandate that custom fields are hashable (i.e., define the __hash__() method), I suppose that most of the existing non-builtin fields are affected.

Looks like the solution is either to keep seen_data as a set() and rely on some property of the field (unreliable and error-prone, see sample code below), or give up set() and manually implement removal of duplicates.

    row_data = tuple(d._get_pk_val() if hasattr(d, '_get_pk_val') else 
                     d if d.__hash__ is not None else
                     for d in row_data)
Last edited 15 months ago by Meiyer (previous) (diff)

comment:17 Changed 15 months ago by Tim Graham

Component: contrib.postgresForms
Description: modified (diff)

I believe it affects all inline formsets, not just those in the admin. There's a PR without tests.

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