﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
3851	unique_together with an empty value for a ForeignKey	rezzrovv <pspierce@…>	nobody	"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['%s__iexact' % 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."		closed	Database layer (models, ORM)	dev		fixed	unique_together		Accepted	1	0	0	0	0	0
