can you help me understand why this code causes a duplicate entry (IntegrityError)?
I'm on Django 1.2.
(row, is_new) = MyModel.objects.get_or_create(field1=1)
row.other_field = 2
row.save()
I do have a unique constraint on field1. If there is a row where field1=1, everything works fine, Django does a "get".
If there is not a row where field1=1, it looks like Django is creating that row which is ok. But why won't it let me save it?
Update:
If it helps, here is MyModel:
class MyModel(models.Model):
id = models.BigIntegerField(primary_key=True)
field1 = models.BigIntegerField(unique=True)
other_field = models.CharField(max_length=765)
class Meta:
db_table = u'project_crosses_suppl_FO'
field1 is a foreign key to another table. But I didn't make a model in Django for that table so I don't tell Django it's a foreign key.