Opened 18 years ago

Closed 16 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: dev
Severity: Keywords: unique_together
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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@…> 18 years ago.
fix unique_together and blank=True ForiegnKey reference
ticket3851.diff (630 bytes ) - added by Christopher Lenz <cmlenz@…> 17 years ago.
Improved patch

Download all attachments as: .zip

Change History (7)

by rezzrovv <pspierce@…>, 18 years ago

Attachment: unique_together_patch.diff added

fix unique_together and blank=True ForiegnKey reference

by Christopher Lenz <cmlenz@…>, 17 years ago

Attachment: ticket3851.diff added

Improved patch

comment:1 by Christopher Lenz <cmlenz@…>, 17 years ago

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 by Jacob, 17 years ago

Triage Stage: UnreviewedAccepted

comment:3 by dirksen, 16 years ago

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.

in reply to:  3 comment:4 by dirksen, 16 years ago

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 by Karen Tracey, 16 years ago

Resolution: fixed
Status: newclosed

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