Ticket #384 (closed defect: fixed)

Opened 6 years ago

Last modified 6 years ago

Adding a OneToOne relation fails

Reported by: david@… Owned by: andrew
Priority: major Milestone: 0.7
Component: migrations Version: 0.7-pre
Keywords: Cc:


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.


south-384-fix.diff (1.1 KB) - added by david@… 6 years ago.
Proposed patch to fix #384

Change History

Changed 6 years ago by david@…

Proposed patch to fix #384

comment:1 Changed 6 years ago by andrew

  • Status changed from new to assigned
  • Milestone set to 0.7

comment:2 Changed 6 years ago by andrew

  • Status changed from assigned to closed
  • Resolution set to fixed

Fixed in [a72454a60f66].

Note: See TracTickets for help on using tickets.