I\'m new to django and trying to have a Foreign key back to users for an assignee and reporter. But when i\'m trying to apply the change with South i get the error
I wasn't using South, but I recently upgraded from Django 1.4 to 1.6 (with MySQL as db backend for both), and was getting the same ValueError
when trying to save some models. I tracked it down to a field that was a recursive ForeignKey. So I had:
class Foo(models.Model):
...
duplicate = models.ForeignKey('self', blank=True, null=True)
...
Somewhere along the line—unfortunately I'm not sure where—many of my objects had gotten the value of 0
for duplicate_id
.
>>> Foo.objects.filter(duplicate_id=0).count()
2078
This didn't occur for any of my other ForeignKey
fields, only the self-referential one. So I set the values of that field back to None
, and this fixed the error.
>>> Foo.objects.filter(duplicate_id=0).update(duplicate=None)
2078L
Because this particular error doesn't point you to a specific field that's causing problems, in general you can check if a ForeignKey field fieldname
has any 0
values:
>>> Foo.objects.filter(fieldname_id=0).count()
If this gives a non-zero result, that field probably needs to be fixed.