Opened 19 years ago
Closed 17 years ago
#3851 closed (fixed)
unique_together with an empty value for a ForeignKey
| Reported by: | 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)
Change History (7)
by , 19 years ago
| Attachment: | unique_together_patch.diff added |
|---|
comment:1 by , 18 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 , 18 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
follow-up: 4 comment:3 by , 17 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.
comment:4 by , 17 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 , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → 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.
fix unique_together and blank=True ForiegnKey reference