Ticket #384 (closed defect: fixed)
Adding a OneToOne relation fails
|Reported by:||david@…||Owned by:||andrew|
My migration basically looks like this:
class Migration(SchemaMigration): def forwards(self, orm): # Adding field 'Account.auth_user' db.add_column(u'account', 'auth_user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['auth.User'], unique=True, null=True), keep_default=False) # Snipped out the frozen ORM from the bottom.
Which looks pretty simple to me, but when it runs I get this error:
django.db.utils.DatabaseError: constraint "auth_user_id_refs_id_1f3e59b6" for relation "account" already exists!
After much fiddling around it seems to me that the SQL that adds the FK constraint is being added to the deferred_sql list and is executed directly, so this results in it being executed twice.
I'm not entirely sure what the correct behaviour should be, but I have a patch that fixes it by checking if the sql has been deferred at the time that it would be executed.
This also failed in a very similar way when I changed the field to a unique ForeignKey? field.