Opened 8 years ago

Closed 7 years ago

#3851 closed (fixed)

unique_together with an empty value for a ForeignKey

Reported by: rezzrovv <pspierce@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: unique_together
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

unique_together with a null value for a ForeignKey causes an "ERROR: invalid input syntax for integer unique_together" when blank=True. The value is set to an empty string in manipulator_validator_unique_together

def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data):
    from django.db.models.fields.related import ManyToOneRel
    from django.utils.text import get_text_list
    field_list = [opts.get_field(field_name) for field_name in field_name_list]
    if isinstance(field_list[0].rel, ManyToOneRel):
        kwargs = {'%s__%s__iexact' % (field_name_list[0], field_list[0].rel.field_name): field_data}
    else:
        kwargs = {'%s__iexact' % field_name_list[0]: field_data}
    for f in field_list[1:]:
        # This is really not going to work for fields that have different
        # form fields, e.g. DateTime.
        # This validation needs to occur after html2python to be effective.
        field_val = all_data.get(f.name, None)
        if field_val is None:
            # This will be caught by another validator, assuming the field
            # doesn't have blank=True.
            return

Since the field_val is coming back as an empty string and not None, kwargs% f.name? = field_val is getting called causing an error when the select statement is executed. The foreignkey is referencing an integer but the select statement gets created w/

"somemodel"."client_id" = ''

causing the type error.

The included patch works for me but I haven't looked into this exhaustively. I'll try and add a regression test when time allows and will include it.

Attachments (2)

unique_together_patch.diff (582 bytes) - added by rezzrovv <pspierce@…> 8 years ago.
fix unique_together and blank=True ForiegnKey reference
ticket3851.diff (630 bytes) - added by Christopher Lenz <cmlenz@…> 8 years ago.
Improved patch

Download all attachments as: .zip

Change History (7)

Changed 8 years ago by rezzrovv <pspierce@…>

fix unique_together and blank=True ForiegnKey reference

Changed 8 years ago by Christopher Lenz <cmlenz@…>

Improved patch

comment:1 Changed 8 years ago by Christopher Lenz <cmlenz@…>

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

I've run into this same issue, and attached an improved patch: it only does the conversion to __isnull for relations, and uses proper comparison.

comment:2 Changed 8 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:3 follow-up: Changed 7 years ago by dirksen

The latest diff fails to address the case of unicode. The test field_val is will fail when field_val has the value of u. Changing it to if field_val == fixes the problem.

comment:4 in reply to: ↑ 3 Changed 7 years ago by dirksen

Replying to dirksen:

The latest diff fails to address the case of unicode. The test field_val is will fail when field_val has the value of u. Changing it to if field_val == fixes the problem.

This comment is a mistake. The latest diff addressed the issue already.

comment:5 Changed 7 years ago by kmtracey

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

Calling this fixed because the manipulator code involved is gone, and I know null ForeignKey values with current forms & unique_together do work OK.

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