Can you help me understand why this code causes a duplicate entry (IntegrityError)?
I am on Django 1.2.
(row, is_new) = MyModel.objects.get_or_create(field1=1) row.other_field = 2 row.save()
I have a unique restriction on field1. If there is a line where field1 = 1, everything works fine, Django does a "get".
If there is no line where field1 = 1, it looks like Django is creating this line, which is fine. But why does this not allow me to keep it?
Update:
If this 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 did not make a model in Django for this table, so I am not telling Django about this foreign key.
source share